1 # -----------------------------------------------------------------------------
2 # Copyright (c) 2014 GarageGames, LLC
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to
6 # deal in the Software without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 # sell copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # -----------------------------------------------------------------------------
23 project("Torque3DEngine")
25 if( CMAKE_CXX_SIZEOF_DATA_PTR EQUAL 8 )
26 set( TORQUE_CPU_X64 ON )
27 elseif( CMAKE_CXX_SIZEOF_DATA_PTR EQUAL 4 )
28 set( TORQUE_CPU_X32 ON )
31 if(NOT TORQUE_TEMPLATE)
32 set(TORQUE_TEMPLATE "Full" CACHE STRING "the template to use")
34 if(NOT TORQUE_APP_DIR)
35 set(TORQUE_APP_DIR "${CMAKE_SOURCE_DIR}/My Projects/${TORQUE_APP_NAME}")
38 set(projectOutDir "${TORQUE_APP_DIR}/game")
41 set(projectSrcDir "${TORQUE_APP_DIR}/source")
43 set(libDir "${CMAKE_SOURCE_DIR}/Engine/lib")
44 set(srcDir "${CMAKE_SOURCE_DIR}/Engine/source")
45 set(cmakeDir "${CMAKE_SOURCE_DIR}/Tools/CMake")
48 mark_as_advanced(CMAKE_INSTALL_PREFIX)
49 mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
52 #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${projectOutDir}/game)
53 #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${projectOutDir}/game)
54 #set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${projectOutDir}/game)
56 ###############################################################################
57 ### Source File Handling
58 ###############################################################################
60 # finds and adds sources files in a folder
64 if(${ARGC} GREATER 1 AND "${ARGV1}" STREQUAL "REC")
65 set(glob_config GLOB_RECURSE)
67 file(${glob_config} tmp_files
74 foreach(entry ${BLACKLIST})
75 list(REMOVE_ITEM tmp_files ${dir}/${entry})
77 LIST(APPEND ${PROJECT_NAME}_files "${tmp_files}")
78 LIST(APPEND ${PROJECT_NAME}_paths "${dir}")
79 #message(STATUS "addPath ${PROJECT_NAME} : ${tmp_files}")
82 # adds a file to the sources
83 macro(addFile filename)
84 LIST(APPEND ${PROJECT_NAME}_files "${filename}")
85 #message(STATUS "addFile ${PROJECT_NAME} : ${filename}")
88 # finds and adds sources files in a folder recursively
90 addPath("${dir}" "REC")
93 ###############################################################################
94 ### Definition Handling
95 ###############################################################################
96 macro(__addDef def config)
97 # two possibilities: a) target already known, so add it directly, or b) target not yet known, so add it to its cache
98 if(TARGET ${PROJECT_NAME})
99 #message(STATUS "directly applying defs: ${PROJECT_NAME} with config ${config}: ${def}")
100 if("${config}" STREQUAL "")
101 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS ${def})
103 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${config}>:${def}>)
106 if("${config}" STREQUAL "")
107 list(APPEND ${PROJECT_NAME}_defs_ ${def})
109 list(APPEND ${PROJECT_NAME}_defs_ $<$<CONFIG:${config}>:${def}>)
111 #message(STATUS "added definition to cache: ${PROJECT_NAME}_defs_: ${${PROJECT_NAME}_defs_}")
115 # adds a definition: argument 1: Nothing(for all), _DEBUG, _RELEASE, <more build configurations>
118 if(${ARGC} GREATER 1)
119 foreach(config ${ARGN})
120 __addDef(${def} ${config})
127 # this applies cached definitions onto the target
129 if(DEFINED ${PROJECT_NAME}_defs_)
130 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS ${${PROJECT_NAME}_defs_})
131 #message(STATUS "applying defs to project ${PROJECT_NAME}: ${${PROJECT_NAME}_defs_}")
135 ###############################################################################
136 ### Source Library Handling
137 ###############################################################################
138 macro(addLibSrc libPath)
139 set(cached_project_name ${PROJECT_NAME})
141 project(${cached_project_name})
144 ###############################################################################
145 ### Linked Library Handling
146 ###############################################################################
149 # check if we can build it ourselfs
150 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/${lib}.cmake")
151 addLibSrc("${CMAKE_CURRENT_SOURCE_DIR}/libraries/${lib}.cmake")
153 # then link against it
154 # two possibilities: a) target already known, so add it directly, or b) target not yet known, so add it to its cache
155 if(TARGET ${PROJECT_NAME})
156 target_link_libraries(${PROJECT_NAME} "${lib}")
158 list(APPEND ${PROJECT_NAME}_libs ${lib})
163 # this applies cached definitions onto the target
165 if(DEFINED ${PROJECT_NAME}_libs)
166 target_link_libraries(${PROJECT_NAME} "${${PROJECT_NAME}_libs}")
170 ###############################################################################
172 ###############################################################################
173 macro(addInclude incPath)
174 if(TARGET ${PROJECT_NAME})
175 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY INCLUDE_DIRECTORIES "${incPath}")
177 list(APPEND ${PROJECT_NAME}_includes ${incPath})
181 # this applies cached definitions onto the target
182 macro(_process_includes)
183 if(DEFINED ${PROJECT_NAME}_includes)
184 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY INCLUDE_DIRECTORIES "${${PROJECT_NAME}_includes}")
188 ###############################################################################
190 macro(_postTargetProcess)
196 # adds a path to search for libs
197 macro(addLibPath dir)
198 link_directories(${dir})
201 # creates a proper filter for VS
202 macro(generateFilters relDir)
203 foreach(f ${${PROJECT_NAME}_files})
204 # Get the path of the file relative to ${DIRECTORY},
205 # then alter it (not compulsory)
206 file(RELATIVE_PATH SRCGR ${relDir} ${f})
207 set(SRCGR "${PROJECT_NAME}/${SRCGR}")
208 # Extract the folder, ie remove the filename part
209 string(REGEX REPLACE "(.*)(/[^/]*)$" "\\1" SRCGR ${SRCGR})
210 # do not have any ../ dirs
211 string(REPLACE "../" "" SRCGR ${SRCGR})
212 # Source_group expects \\ (double antislash), not / (slash)
213 string(REPLACE / \\ SRCGR ${SRCGR})
214 #STRING(REPLACE "//" "/" SRCGR ${SRCGR})
215 #message(STATUS "FILE: ${f} -> ${SRCGR}")
216 source_group("${SRCGR}" FILES ${f})
220 # creates a proper filter for VS
221 macro(generateFiltersSpecial relDir)
222 foreach(f ${${PROJECT_NAME}_files})
223 # Get the path of the file relative to ${DIRECTORY},
224 # then alter it (not compulsory)
225 file(RELATIVE_PATH SRCGR ${relDir} ${f})
226 set(SRCGR "torque3d/${SRCGR}")
227 # Extract the folder, ie remove the filename part
228 string(REGEX REPLACE "(.*)(/[^/]*)$" "\\1" SRCGR ${SRCGR})
229 # do not have any ../ dirs
230 string(REPLACE "../" "" SRCGR ${SRCGR})
231 IF("${SRCGR}" MATCHES "^torque3d/My Projects/.*$")
232 string(REPLACE "torque3d/My Projects/${PROJECT_NAME}/" "" SRCGR ${SRCGR})
233 string(REPLACE "/source" "" SRCGR ${SRCGR})
235 # Source_group expects \\ (double antislash), not / (slash)
236 string(REPLACE / \\ SRCGR ${SRCGR})
237 #STRING(REPLACE "//" "/" SRCGR ${SRCGR})
238 IF(EXISTS "${f}" AND NOT IS_DIRECTORY "${f}")
239 #message(STATUS "FILE: ${f} -> ${SRCGR}")
240 source_group("${SRCGR}" FILES ${f})
245 # macro to add a static library
248 if(${ARGC} GREATER 0)
249 foreach(dir ${ARGV0})
253 # now inspect the paths we got
255 foreach(dir ${${PROJECT_NAME}_paths})
256 if("${firstDir}" STREQUAL "")
257 set(firstDir "${dir}")
260 generateFilters("${firstDir}")
262 # set per target compile flags
263 if(TORQUE_CXX_FLAGS_${PROJECT_NAME})
264 set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_${PROJECT_NAME}}")
266 set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_LIBS}")
270 add_library("${PROJECT_NAME}" STATIC ${${PROJECT_NAME}_files})
272 add_library("${PROJECT_NAME}" SHARED ${${PROJECT_NAME}_files})
275 # omg - only use the first folder ... otherwise we get lots of header name collisions
276 #foreach(dir ${${PROJECT_NAME}_paths})
277 addInclude("${firstDir}")
283 # macro to add an executable
284 macro(finishExecutable)
285 # now inspect the paths we got
287 foreach(dir ${${PROJECT_NAME}_paths})
288 if("${firstDir}" STREQUAL "")
289 set(firstDir "${dir}")
292 generateFiltersSpecial("${firstDir}")
294 # set per target compile flags
295 if(TORQUE_CXX_FLAGS_${PROJECT_NAME})
296 set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_${PROJECT_NAME}}")
298 set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_EXECUTABLES}")
301 add_executable("${PROJECT_NAME}" WIN32 ${${PROJECT_NAME}_files})
302 addInclude("${firstDir}")
307 macro(setupVersionNumbers)
308 set(TORQUE_APP_VERSION_MAJOR 1 CACHE INTEGER "")
309 set(TORQUE_APP_VERSION_MINOR 0 CACHE INTEGER "")
310 set(TORQUE_APP_VERSION_PATCH 0 CACHE INTEGER "")
311 set(TORQUE_APP_VERSION_TWEAK 0 CACHE INTEGER "")
312 mark_as_advanced(TORQUE_APP_VERSION_TWEAK)
313 MATH(EXPR TORQUE_APP_VERSION "${TORQUE_APP_VERSION_MAJOR} * 1000 + ${TORQUE_APP_VERSION_MINOR} * 100 + ${TORQUE_APP_VERSION_PATCH} * 10 + ${TORQUE_APP_VERSION_TWEAK}")
314 set(TORQUE_APP_VERSION_STRING "${TORQUE_APP_VERSION_MAJOR}.${TORQUE_APP_VERSION_MINOR}.${TORQUE_APP_VERSION_PATCH}.${TORQUE_APP_VERSION_TWEAK}")
315 #message(STATUS "version numbers: ${TORQUE_APP_VERSION} / ${TORQUE_APP_VERSION_STRING}")
318 macro(setupPackaging)
320 # only enable zips for now
321 set(CPACK_BINARY_NSIS OFF CACHE INTERNAL "" FORCE)
322 set(CPACK_BINARY_ZIP ON CACHE INTERNAL "" FORCE)
323 set(CPACK_SOURCE_ZIP OFF CACHE INTERNAL "" FORCE)
324 SET(CPACK_GENERATOR "ZIP")
325 SET(CPACK_PACKAGE_VENDOR "${PROJECT_NAME}")
326 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_NAME}")
327 SET(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 1)
328 SET(CPACK_OUTPUT_FILE_PREFIX "${TORQUE_APP_DIR}/packages/${PROJECT_NAME}")
329 SET(CPACK_PACKAGE_INSTALL_DIRECTORY "")
330 #SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ReadMe.txt")
331 #SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt")
332 SET(CPACK_PACKAGE_VERSION_MAJOR "${TORQUE_APP_VERSION_MAJOR}")
333 SET(CPACK_PACKAGE_VERSION_MINOR "${TORQUE_APP_VERSION_MINOR}")
334 SET(CPACK_PACKAGE_VERSION_PATCH "${TORQUE_APP_VERSION_PATCH}")
335 #SET(CPACK_PACKAGE_EXECUTABLES "${PROJECT_NAME}" "${PROJECT_NAME}")
336 SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${TORQUE_APP_VERSION_STRING}")
337 #SET(CPACK_SOURCE_STRIP_FILES "")
339 # always static for now
340 set(TORQUE_STATIC ON)
341 #option(TORQUE_STATIC "enables or disable static" OFF)
344 set(TORQUE_CXX_FLAGS_EXECUTABLES "/wd4018 /wd4100 /wd4121 /wd4127 /wd4130 /wd4244 /wd4245 /wd4389 /wd4511 /wd4512 /wd4800 /wd4995 " CACHE TYPE STRING)
345 mark_as_advanced(TORQUE_CXX_FLAGS_EXECUTABLES)
347 set(TORQUE_CXX_FLAGS_LIBS "/W0" CACHE TYPE STRING)
348 mark_as_advanced(TORQUE_CXX_FLAGS_LIBS)
350 set(TORQUE_CXX_FLAGS_COMMON_DEFAULT "-DUNICODE -D_UNICODE -D_CRT_SECURE_NO_WARNINGS /MP /O2 /Ob2 /Oi /Ot /Oy /GT /Zi /W4 /nologo /GF /EHsc /GS- /Gy- /Qpar- /fp:fast /fp:except- /GR /Zc:wchar_t-" )
352 set(TORQUE_CXX_FLAGS_COMMON_DEFAULT "${TORQUE_CXX_FLAGS_COMMON_DEFAULT} /arch:SSE2")
354 set(TORQUE_CXX_FLAGS_COMMON ${TORQUE_CXX_FLAGS_COMMON_DEFAULT} CACHE TYPE STRING)
356 mark_as_advanced(TORQUE_CXX_FLAGS_COMMON)
358 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORQUE_CXX_FLAGS_COMMON}")
359 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
360 set(CMAKE_EXE_LINKER_FLAGS "/LARGEADDRESSAWARE")
361 #set(STATIC_LIBRARY_FLAGS "/OPT:NOREF")
363 # Force static runtime libraries
366 CMAKE_C_FLAGS_RELEASE
367 CMAKE_C_FLAGS_RELWITHDEBINFO
369 CMAKE_C_FLAGS_DEBUG_INIT
370 CMAKE_CXX_FLAGS_RELEASE
371 CMAKE_CXX_FLAGS_RELWITHDEBINFO
372 CMAKE_CXX_FLAGS_DEBUG
373 CMAKE_CXX_FLAGS_DEBUG_INIT)
374 STRING(REPLACE "/MD" "/MT" "${flag}" "${${flag}}")
375 SET("${flag}" "${${flag}} /EHsc")
379 # TODO: improve default settings on other platforms
380 set(TORQUE_CXX_FLAGS_EXECUTABLES "" CACHE TYPE STRING)
381 mark_as_advanced(TORQUE_CXX_FLAGS_EXECUTABLES)
382 set(TORQUE_CXX_FLAGS_LIBS "" CACHE TYPE STRING)
383 mark_as_advanced(TORQUE_CXX_FLAGS_LIBS)
384 set(TORQUE_CXX_FLAGS_COMMON "" CACHE TYPE STRING)
385 mark_as_advanced(TORQUE_CXX_FLAGS)
386 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORQUE_CXX_FLAGS}")
387 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
391 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${projectOutDir}")
392 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${projectOutDir}")
393 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${projectOutDir}")
394 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${projectOutDir}")
397 # fix the debug/release subfolders on windows
399 SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY" "${projectOutDir}")
400 FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
401 # Go uppercase (DEBUG, RELEASE...)
402 STRING(TOUPPER "${CONF}" CONF)
403 #SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${projectOutDir}")
404 SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${projectOutDir}")