Integrates the nativeFileDialog library to enable native file dialogs on the major...
[Torque-3d.git] / Tools / CMake / basics.cmake
blobf7c0d9442a21557d7bc038fb2203217160fea211
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
20 # IN THE SOFTWARE.
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 )
29 endif()
31 if(NOT TORQUE_TEMPLATE)
32     set(TORQUE_TEMPLATE "Full" CACHE STRING "the template to use")
33 endif()
34 if(NOT TORQUE_APP_DIR)
35     set(TORQUE_APP_DIR "${CMAKE_SOURCE_DIR}/My Projects/${TORQUE_APP_NAME}")
36 endif()
37 if(NOT projectOutDir)
38     set(projectOutDir "${TORQUE_APP_DIR}/game")
39 endif()
40 if(NOT projectSrcDir)
41     set(projectSrcDir "${TORQUE_APP_DIR}/source")
42 endif()
43 set(libDir        "${CMAKE_SOURCE_DIR}/Engine/lib")
44 set(srcDir        "${CMAKE_SOURCE_DIR}/Engine/source")
45 set(cmakeDir      "${CMAKE_SOURCE_DIR}/Tools/CMake")
47 # hide some things
48 mark_as_advanced(CMAKE_INSTALL_PREFIX)
49 mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
51 # output folders
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
61 macro(addPath dir)
62     set(tmp_files "")
63     set(glob_config GLOB)
64     if(${ARGC} GREATER 1 AND "${ARGV1}" STREQUAL "REC")
65         set(glob_config GLOB_RECURSE)
66     endif()
67     file(${glob_config} tmp_files
68              ${dir}/*.cpp
69              ${dir}/*.c
70              ${dir}/*.cc
71              ${dir}/*.h
72              #${dir}/*.asm
73              )
74     foreach(entry ${BLACKLIST})
75                 list(REMOVE_ITEM tmp_files ${dir}/${entry})
76         endforeach()
77     LIST(APPEND ${PROJECT_NAME}_files "${tmp_files}")
78     LIST(APPEND ${PROJECT_NAME}_paths "${dir}")
79     #message(STATUS "addPath ${PROJECT_NAME} : ${tmp_files}")
80 endmacro()
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}")
86 endmacro()
88 # finds and adds sources files in a folder recursively
89 macro(addPathRec dir)
90     addPath("${dir}" "REC")
91 endmacro()
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})
102         else()
103             set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${config}>:${def}>)
104         endif()
105     else()
106         if("${config}" STREQUAL "")
107             list(APPEND ${PROJECT_NAME}_defs_ ${def})
108         else()
109             list(APPEND ${PROJECT_NAME}_defs_ $<$<CONFIG:${config}>:${def}>)
110         endif()
111         #message(STATUS "added definition to cache: ${PROJECT_NAME}_defs_: ${${PROJECT_NAME}_defs_}")
112     endif()
113 endmacro()
115 # adds a definition: argument 1: Nothing(for all), _DEBUG, _RELEASE, <more build configurations>
116 macro(addDef def)
117     set(def_configs "")
118     if(${ARGC} GREATER 1)
119         foreach(config ${ARGN})
120             __addDef(${def} ${config})
121         endforeach()
122     else()
123         __addDef(${def} "")
124     endif()
125 endmacro()
127 # this applies cached definitions onto the target
128 macro(_process_defs)
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_}")
132     endif()
133 endmacro()
135 ###############################################################################
136 ###  Source Library Handling
137 ###############################################################################
138 macro(addLibSrc libPath)
139     set(cached_project_name ${PROJECT_NAME})
140     include(${libPath})
141     project(${cached_project_name})
142 endmacro()
144 ###############################################################################
145 ### Linked Library Handling
146 ###############################################################################
147 macro(addLib libs)
148    foreach(lib ${libs})
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")
152         endif()
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}")
157         else()
158             list(APPEND ${PROJECT_NAME}_libs ${lib})
159         endif()
160    endforeach()
161 endmacro()
163 # this applies cached definitions onto the target
164 macro(_process_libs)
165     if(DEFINED ${PROJECT_NAME}_libs)
166         target_link_libraries(${PROJECT_NAME} "${${PROJECT_NAME}_libs}")
167     endif()
168 endmacro()
170 ###############################################################################
171 ### Include Handling
172 ###############################################################################
173 macro(addInclude incPath)
174     if(TARGET ${PROJECT_NAME})
175         set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY INCLUDE_DIRECTORIES "${incPath}")
176     else()
177         list(APPEND ${PROJECT_NAME}_includes ${incPath})
178     endif()
179 endmacro()
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}")
185     endif()
186 endmacro()
188 ###############################################################################
190 macro(_postTargetProcess)
191     _process_includes()
192     _process_defs()
193     _process_libs()
194 endmacro()
196 # adds a path to search for libs
197 macro(addLibPath dir)
198     link_directories(${dir})
199 endmacro()
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})
217     endforeach()
218 endmacro()
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})
234         endif()
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})
241         endif()
242     endforeach()
243 endmacro()
245 # macro to add a static library
246 macro(finishLibrary)
247     # more paths?
248     if(${ARGC} GREATER 0)
249         foreach(dir ${ARGV0})
250             addPath("${dir}")
251         endforeach()
252     endif()
253     # now inspect the paths we got
254     set(firstDir "")
255     foreach(dir ${${PROJECT_NAME}_paths})
256         if("${firstDir}" STREQUAL "")
257             set(firstDir "${dir}")
258         endif()
259     endforeach()
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}}")
265     else()
266         set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_LIBS}")
267     endif()
269     if(TORQUE_STATIC)
270         add_library("${PROJECT_NAME}" STATIC ${${PROJECT_NAME}_files})
271     else()
272         add_library("${PROJECT_NAME}" SHARED ${${PROJECT_NAME}_files})
273     endif()
275     # omg - only use the first folder ... otherwise we get lots of header name collisions
276     #foreach(dir ${${PROJECT_NAME}_paths})
277     addInclude("${firstDir}")
278     #endforeach()
280     _postTargetProcess()
281 endmacro()
283 # macro to add an executable
284 macro(finishExecutable)
285     # now inspect the paths we got
286     set(firstDir "")
287     foreach(dir ${${PROJECT_NAME}_paths})
288         if("${firstDir}" STREQUAL "")
289             set(firstDir "${dir}")
290         endif()
291     endforeach()
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}}")
297     else()
298         set_source_files_properties(${${PROJECT_NAME}_files} PROPERTIES COMPILE_FLAGS "${TORQUE_CXX_FLAGS_EXECUTABLES}")
299     endif()
301     add_executable("${PROJECT_NAME}" WIN32 ${${PROJECT_NAME}_files})
302     addInclude("${firstDir}")
304     _postTargetProcess()
305 endmacro()
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}")
316 endmacro()
318 macro(setupPackaging)
319     INCLUDE(CPack)
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 "")
338 endmacro()
339 # always static for now
340 set(TORQUE_STATIC ON)
341 #option(TORQUE_STATIC "enables or disable static" OFF)
343 if(WIN32)
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-" )
351     if( TORQUE_CPU_X32 )
352        set(TORQUE_CXX_FLAGS_COMMON_DEFAULT "${TORQUE_CXX_FLAGS_COMMON_DEFAULT} /arch:SSE2")
353     endif()
354     set(TORQUE_CXX_FLAGS_COMMON ${TORQUE_CXX_FLAGS_COMMON_DEFAULT} CACHE TYPE STRING)
355     
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
364     if(TORQUE_STATIC)
365         FOREACH(flag
366             CMAKE_C_FLAGS_RELEASE
367             CMAKE_C_FLAGS_RELWITHDEBINFO
368             CMAKE_C_FLAGS_DEBUG
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")
376         ENDFOREACH()
377     endif()
378 else()
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}")
388 endif()
390 if(UNIX)
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}")
395 endif()
397 # fix the debug/release subfolders on windows
398 if(MSVC)
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}")
405     ENDFOREACH()
406 endif()