55# seriously) modIFied by Ronan Collobert, for Torch7
66
77if (NOT DEFINED BUNDLE_CMD)
8- set (BUNDLE_CMD luajit CACHE STRING "Use lua to do lua file bundle" )
8+ if (BUNDLE_USE_LUA2C)
9+ set (BUNDLE_CMD lua CACHE STRING "Use lua to do lua file bundle" )
10+ else ()
11+ set (BUNDLE_CMD luajit CACHE STRING "Use luajit to do lua file bundle" )
12+ endif ()
13+ endif ()
14+ if (DEFINED ENV{BUNDLE_CMD})
15+ set (BUNDLE_CMD $ENV{BUNDLE_CMD} )
916endif ()
17+
1018if (NOT DEFINED BUNDLE_CMD_ARGS)
1119 set (BUNDLE_CMD_ARGS "" CACHE STRING "Bundle args for cross compile" )
1220endif ()
@@ -17,160 +25,77 @@ endif()
1725include (TestBigEndian)
1826TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
1927if (IS_BIG_ENDIAN)
20- message (STATUS "BIG_ENDIAN" )
28+ message (STATUS "BIG_ENDIAN" )
2129else ()
22- message (STATUS "LITTLE_ENDIAN" )
30+ message (STATUS "LITTLE_ENDIAN" )
2331endif ()
2432
25- if (BUNDLE_USE_LUA2C)
26- file (COPY ${CMAKE_CURRENT_LIST_DIR} /lua2c.lua DESTINATION ${LUA_TARGET_PATH} )
27- endif ()
28-
29- macro (LUA_add_custom_commands luajit_target)
30- set (target_srcs "" )
31- foreach (file ${ARGN} )
32- if (${file} MATCHES ".*\\ .lua$" )
33- if (NOT IS_ABSOLUTE ${file} )
34- set (file "${CMAKE_CURRENT_SOURCE_DIR} /${file} " )
35- endif ()
36- set (source_file ${file} )
37- string (LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
38- string (LENGTH ${file} _luajit_file_length)
39- math (EXPR _begin "${_luajit_source_dir_length} + 1" )
40- math (EXPR _stripped_file_length
41- "${_luajit_file_length} - ${_luajit_source_dir_length} - 1" )
42- string (SUBSTRING ${file}
43- ${_begin}
44- ${_stripped_file_length}
45- stripped_file)
46-
47- set (
48- generated_file
49- "${CMAKE_BINARY_DIR} /luacode_tmp/${stripped_file} _${luajit_target} _generated.c"
50- )
51-
52- add_custom_command (
53- OUTPUT ${generated_file}
54- MAIN_DEPENDENCY ${source_file}
55- DEPENDS ${LUA_TARGET}
56- COMMAND ${BUNDLE_CMD} ARGS
57- ${BUNDLE_CMD_ARGS} lua2c.lua ${source_file} ${generated_file}
58- COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} lua2c.lua ${source_file} ${generated_file} "
59- WORKING_DIRECTORY ${LUA_TARGET_PATH} )
60-
61- get_filename_component (basedir ${generated_file} PATH )
62- file (MAKE_DIRECTORY ${basedir} )
63-
64- set (target_srcs ${target_srcs} ${generated_file} )
65- set_source_files_properties (${generated_file}
66- properties
67- generated
68- true # to say that "it is OK that the obj-
69- # files do not exist before build time"
70- )
71- else ()
72- set (target_srcs ${target_srcs} ${file} )
33+ if (NOT DEFINED BUNDLE_DEBUG)
34+ if (CMAKE_BUILD_TYPE )
35+ string (TOLOWER "${CMAKE_BUILD_TYPE} " CMAKE_BUILD_TYPE_LOWER)
36+ if (${CMAKE_BUILD_TYPE_LOWER} STREQUAL "debug"
37+ OR ${CMAKE_BUILD_TYPE_LOWER} STREQUAL "relwithdebinfo" )
38+ set (BUNDLE_DEBUG ON )
7339 endif ()
74- endforeach ()
75- endmacro ()
76-
77- if (NOT LJ_TARGET_ARCH)
78- include (${CMAKE_CURRENT_LIST_DIR} /Modules/DetectArchitecture.cmake)
79- detect_architecture(LJ_DETECTED_ARCH)
80- if ("${LJ_DETECTED_ARCH} " STREQUAL "x86" )
81- set (LJ_TARGET_ARCH "x86" )
82- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "x86_64" )
83- set (LJ_TARGET_ARCH "x64" )
84- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "AArch64" )
85- set (LJ_TARGET_ARCH "arm64" )
86- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "ARM" )
87- set (LJ_TARGET_ARCH "arm" )
88- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "Mips64" )
89- if (IS_BIG_ENDIAN)
90- set (LJ_TARGET_ARCH "mips64" )
91- else ()
92- set (LJ_TARGET_ARCH "mips64el" )
93- endif ()
94- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "Mips" )
95- if (IS_BIG_ENDIAN)
96- set (LJ_TARGET_ARCH "mips" )
97- else ()
98- set (LJ_TARGET_ARCH "mipsel" )
99- endif ()
100- elseif ("${LJ_DETECTED_ARCH} " STREQUAL "PowerPC" )
101- if (LJ_64)
102- set (LJ_TARGET_ARCH "ppc64" )
10340 else ()
104- set (LJ_TARGET_ARCH "ppc" )
41+ set (BUNDLE_DEBUG OFF )
10542 endif ()
106- else ()
107- message (FATAL_ERROR "Unsupported target architecture: '${LJ_DETECTED_ARCH} '" )
108- endif ()
10943endif ()
11044
111- macro (LUAJIT_add_custom_commands luajit_target)
112- set (target_srcs "" )
113-
114- if (WIN32 )
115- set (LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o windows)
116- elseif (APPLE )
117- set (LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o osx)
118- elseif (ANDROID OR ${CMAKE_SYSTEM_NAME} STREQUAL Linux)
119- set (LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o linux)
120- else ()
121- set (LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} )
122- endif ()
45+ if (BUNDLE_USE_LUA2C)
46+ file (COPY ${CMAKE_CURRENT_LIST_DIR} /lua2c.lua DESTINATION ${LUA_TARGET_PATH} )
12347
124- foreach (file ${ARGN} )
125- if (${file} MATCHES ".*\\ .lua$" )
126- if (NOT IS_ABSOLUTE ${file} )
127- set (file "${CMAKE_CURRENT_SOURCE_DIR} /${file} " )
48+ if (BUNDLE_DEBUG)
49+ set (BUNDLE_ENABLE_DEBUG "-g" )
50+ endif ()
51+
52+ macro (LUA_add_custom_commands luajit_target)
53+ set (target_srcs "" )
54+ foreach (file ${ARGN} )
55+ if (${file} MATCHES ".*\\ .lua$" )
56+ if (NOT IS_ABSOLUTE ${file} )
57+ set (file "${CMAKE_CURRENT_SOURCE_DIR} /${file} " )
58+ endif ()
59+ set (source_file ${file} )
60+ string (LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
61+ string (LENGTH ${file} _luajit_file_length)
62+ math (EXPR _begin "${_luajit_source_dir_length} + 1" )
63+ math (EXPR _stripped_file_length
64+ "${_luajit_file_length} - ${_luajit_source_dir_length} - 1" )
65+ string (SUBSTRING ${file}
66+ ${_begin}
67+ ${_stripped_file_length}
68+ stripped_file)
69+
70+ set (
71+ generated_file
72+ "${CMAKE_BINARY_DIR} /luacode_tmp/${stripped_file} _${luajit_target} _generated.c"
73+ )
74+
75+ add_custom_command (
76+ OUTPUT ${generated_file}
77+ MAIN_DEPENDENCY ${source_file}
78+ DEPENDS ${LUA_TARGET}
79+ COMMAND ${BUNDLE_CMD} ARGS
80+ ${BUNDLE_CMD_ARGS} lua2c.lua ${BUNDLE_ENABLE_DEBUG} ${source_file} ${generated_file}
81+ COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} lua2c.lua ${BUNDLE_ENABLE_DEBUG} ${source_file} ${generated_file} "
82+ WORKING_DIRECTORY ${LUA_TARGET_PATH} )
83+
84+ get_filename_component (basedir ${generated_file} PATH )
85+ file (MAKE_DIRECTORY ${basedir} )
86+
87+ set (target_srcs ${target_srcs} ${generated_file} )
88+ set_source_files_properties (${generated_file}
89+ properties
90+ generated
91+ true # to say that "it is OK that the obj-
92+ # files do not exist before build time"
93+ )
94+ else ()
95+ set (target_srcs ${target_srcs} ${file} )
12896 endif ()
129- set (source_file ${file} )
130- string (LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
131- string (LENGTH ${file} _luajit_file_length)
132- math (EXPR _begin "${_luajit_source_dir_length} + 1" )
133- math (EXPR _stripped_file_length
134- "${_luajit_file_length} - ${_luajit_source_dir_length} - 1" )
135- string (SUBSTRING ${file}
136- ${_begin}
137- ${_stripped_file_length}
138- stripped_file)
139-
140- set (
141- generated_file
142- "${CMAKE_CURRENT_BINARY_DIR} /jitted_tmp/${stripped_file} _${luajit_target} _generated${CMAKE_C_OUTPUT_EXTENSION} "
143- )
144- string (REPLACE ";" " " LJDUMP_OPT_STR "${LJDUMP_OPT} " )
145-
146- add_custom_command (
147- OUTPUT ${generated_file}
148- MAIN_DEPENDENCY ${source_file}
149- DEPENDS ${LUA_TARGET}
150- COMMAND ${BUNDLE_CMD} ARGS
151- ${BUNDLE_CMD_ARGS}
152- ${LJDUMP_OPT} ${source_file} ${generated_file}
153- COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} ${LJDUMP_OPT_STR} ${source_file} ${generated_file} "
154- WORKING_DIRECTORY ${LUA_TARGET_PATH} )
155- get_filename_component (basedir ${generated_file} PATH )
156- file (MAKE_DIRECTORY ${basedir} )
157-
158- set (target_srcs ${target_srcs} ${generated_file} )
159- set_source_files_properties (${generated_file}
160- properties
161- external_object
162- true # this is an object file
163- generated
164- true # to say that "it is OK that the obj-
165- # files do not exist before build time"
166- )
167- else ()
168- set (target_srcs ${target_srcs} ${file} )
169- endif ()
170- endforeach ()
171- endmacro ()
172-
173- if (BUNDLE_USE_LUA2C)
97+ endforeach ()
98+ endmacro ()
17499
175100 macro (LUA_ADD_CUSTOM luajit_target)
176101 lua_add_custom_commands(${luajit_target} ${ARGN} )
@@ -180,9 +105,109 @@ if(BUNDLE_USE_LUA2C)
180105 lua_add_custom_commands(${luajit_target} ${ARGN} )
181106 add_executable (${luajit_target} ${target_srcs} )
182107 endmacro ()
183-
184108else ()
185109
110+ if (NOT LJ_TARGET_ARCH)
111+ include (${CMAKE_CURRENT_LIST_DIR} /Modules/DetectArchitecture.cmake)
112+ detect_architecture(LJ_DETECTED_ARCH)
113+ if ("${LJ_DETECTED_ARCH} " STREQUAL "x86" )
114+ set (LJ_TARGET_ARCH "x86" )
115+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "x86_64" )
116+ set (LJ_TARGET_ARCH "x64" )
117+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "AArch64" )
118+ set (LJ_TARGET_ARCH "arm64" )
119+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "ARM" )
120+ set (LJ_TARGET_ARCH "arm" )
121+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "Loongarch64" )
122+ set (LJ_TARGET_ARCH "Loongarch64" )
123+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "Mips64" )
124+ if (IS_BIG_ENDIAN)
125+ set (LJ_TARGET_ARCH "mips64" )
126+ else ()
127+ set (LJ_TARGET_ARCH "mips64el" )
128+ endif ()
129+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "Mips" )
130+ if (IS_BIG_ENDIAN)
131+ set (LJ_TARGET_ARCH "mips" )
132+ else ()
133+ set (LJ_TARGET_ARCH "mipsel" )
134+ endif ()
135+ elseif ("${LJ_DETECTED_ARCH} " STREQUAL "PowerPC" )
136+ if (LJ_64)
137+ set (LJ_TARGET_ARCH "ppc64" )
138+ else ()
139+ set (LJ_TARGET_ARCH "ppc" )
140+ endif ()
141+ else ()
142+ message (FATAL_ERROR "Unsupported target architecture: '${LJ_DETECTED_ARCH} '" )
143+ endif ()
144+ endif ()
145+
146+ macro (LUAJIT_add_custom_commands luajit_target)
147+ set (target_srcs "" )
148+
149+ if (WIN32 )
150+ set (LJDUMP_OPT -b -a ${LJ_TARGET_ARCH} -o windows)
151+ elseif (APPLE )
152+ set (LJDUMP_OPT -b -a ${LJ_TARGET_ARCH} -o osx)
153+ elseif (ANDROID OR ${CMAKE_SYSTEM_NAME} STREQUAL Linux)
154+ set (LJDUMP_OPT -b -a ${LJ_TARGET_ARCH} -o linux)
155+ else ()
156+ set (LJDUMP_OPT -b -a ${LJ_TARGET_ARCH} )
157+ endif ()
158+ if (BUNDLE_DEBUG)
159+ list (APPEND LJDUMP_OPT -g)
160+ endif ()
161+
162+ foreach (file ${ARGN} )
163+ if (${file} MATCHES ".*\\ .lua$" )
164+ if (NOT IS_ABSOLUTE ${file} )
165+ set (file "${CMAKE_CURRENT_SOURCE_DIR} /${file} " )
166+ endif ()
167+ set (source_file ${file} )
168+ string (LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
169+ string (LENGTH ${file} _luajit_file_length)
170+ math (EXPR _begin "${_luajit_source_dir_length} + 1" )
171+ math (EXPR _stripped_file_length
172+ "${_luajit_file_length} - ${_luajit_source_dir_length} - 1" )
173+ string (SUBSTRING ${file}
174+ ${_begin}
175+ ${_stripped_file_length}
176+ stripped_file)
177+
178+ set (
179+ generated_file
180+ "${CMAKE_CURRENT_BINARY_DIR} /jitted_tmp/${stripped_file} _${luajit_target} _generated${CMAKE_C_OUTPUT_EXTENSION} "
181+ )
182+ string (REPLACE ";" " " LJDUMP_OPT_STR "${LJDUMP_OPT} " )
183+
184+ add_custom_command (
185+ OUTPUT ${generated_file}
186+ MAIN_DEPENDENCY ${source_file}
187+ DEPENDS ${LUA_TARGET}
188+ COMMAND ${BUNDLE_CMD} ARGS
189+ ${BUNDLE_CMD_ARGS}
190+ ${LJDUMP_OPT} ${source_file} ${generated_file}
191+ COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} ${LJDUMP_OPT_STR} ${source_file} ${generated_file} "
192+ WORKING_DIRECTORY ${LUA_TARGET_PATH} )
193+ get_filename_component (basedir ${generated_file} PATH )
194+ file (MAKE_DIRECTORY ${basedir} )
195+
196+ set (target_srcs ${target_srcs} ${generated_file} )
197+ set_source_files_properties (${generated_file}
198+ properties
199+ external_object
200+ true # this is an object file
201+ generated
202+ true # to say that "it is OK that the obj-
203+ # files do not exist before build time"
204+ )
205+ else ()
206+ set (target_srcs ${target_srcs} ${file} )
207+ endif ()
208+ endforeach ()
209+ endmacro ()
210+
186211 macro (LUA_ADD_CUSTOM luajit_target)
187212 luajit_add_custom_commands(${luajit_target} ${ARGN} )
188213 endmacro ()
0 commit comments