CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #
  2. # Copyright (c) 2008-2014 the Urho3D project.
  3. #
  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 deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  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 FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Define target name
  23. set (TARGET_NAME Urho3D)
  24. if (WIN32)
  25. set (CMAKE_DEBUG_POSTFIX _d)
  26. endif ()
  27. # Define generated source files
  28. add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/librevision.h
  29. COMMAND ${CMAKE_COMMAND} -DFILENAME=${CMAKE_CURRENT_BINARY_DIR}/librevision.h -P CMake/Modules/GetUrho3DRevision.cmake
  30. DEPENDS ${STATIC_LIBRARY_TARGETS} ${PROJECT_SOURCE_DIR}/CMake/Modules/GetUrho3DRevision.cmake
  31. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  32. COMMENT "Generating GIT revision number (tag + last commit SHA-1)")
  33. # Define generated object files
  34. # This is a hack as it relies on internal working of CMake
  35. if (MSVC AND URHO3D_LIB_TYPE STREQUAL SHARED) # MSVC linker does not have force_load/whole_archive equivalent
  36. foreach (TARGET ${STATIC_LIBRARY_TARGETS})
  37. get_target_property (SOURCES ${TARGET} SOURCES)
  38. get_target_property (INT_DIR ${TARGET} LOCATION)
  39. get_filename_component (INT_DIR ${INT_DIR} PATH)
  40. if (CMAKE_GENERATOR MATCHES 2008)
  41. string (REPLACE /$(OutDir) /${TARGET}.dir/$(ConfigurationName) INT_DIR ${INT_DIR})
  42. else ()
  43. string (REPLACE /$(Configuration) /${TARGET}.dir/$(ConfigurationName) INT_DIR ${INT_DIR})
  44. endif ()
  45. foreach (SOURCE ${SOURCES})
  46. get_filename_component (NAME ${SOURCE} NAME)
  47. if (NAME MATCHES \\.c.*$|\\.mm?$|\\.S$|\\.s$)
  48. string (REGEX REPLACE \\.c.*$|\\.mm?$|\\.S$|\\.s$ "" NAME ${NAME})
  49. list (APPEND OBJ_FILES ${INT_DIR}/${NAME}.obj)
  50. elseif (NAME MATCHES \\.o.*$)
  51. list (APPEND OBJ_FILES ${SOURCE})
  52. endif ()
  53. endforeach ()
  54. source_group ("Object Files\\${TARGET}" FILES ${OBJ_FILES})
  55. list (APPEND ALL_OBJ_FILES ${OBJ_FILES})
  56. unset (OBJ_FILES)
  57. endforeach ()
  58. endif ()
  59. # Define source files
  60. set (SOURCES Audio Container Core Engine Graphics Input IO Math Resource Scene UI)
  61. if (URHO3D_ANGELSCRIPT)
  62. list (APPEND SOURCES Script)
  63. endif ()
  64. if (URHO3D_NETWORK)
  65. list (APPEND SOURCES Network)
  66. endif ()
  67. if (URHO3D_NAVIGATION)
  68. list (APPEND SOURCES Navigation)
  69. endif ()
  70. if (URHO3D_PHYSICS)
  71. list (APPEND SOURCES Physics)
  72. endif ()
  73. if (URHO3D_URHO2D)
  74. list (APPEND SOURCES Urho2D)
  75. list (APPEND SOURCES UIX)
  76. endif ()
  77. foreach (SOURCE ${SOURCES})
  78. add_subdirectory (${SOURCE})
  79. install (DIRECTORY ${SOURCE}/ DESTINATION ${DEST_INCLUDE_DIR} ${DEST_PERMISSIONS} FILES_MATCHING PATTERN *.h) # Note: the trailing slash is significant
  80. source_group ("Source Files\\${SOURCE}" FILES ${${SOURCE}_CPP_FILES})
  81. source_group ("Header Files\\${SOURCE}" FILES ${${SOURCE}_H_FILES})
  82. list (APPEND ENGINE_SOURCE_FILES ${${SOURCE}_CPP_FILES} ${${SOURCE}_H_FILES})
  83. list (APPEND ENGINE_INCLUDE_DIRS_ONLY ${SOURCE})
  84. endforeach ()
  85. define_source_files (EXTRA_CPP_FILES ${ENGINE_SOURCE_FILES} EXTRA_H_FILES ${CMAKE_CURRENT_BINARY_DIR}/librevision.h PCH)
  86. install (FILES ${H_FILES} DESTINATION ${DEST_INCLUDE_DIR})
  87. list (APPEND SOURCE_FILES ${ALL_OBJ_FILES})
  88. set_source_files_properties (${ALL_OBJ_FILES} PROPERTIES GENERATED TRUE)
  89. # Define dependency libs
  90. foreach (LIST_NAME LIBS INCLUDE_DIRS_ONLY LINK_LIBS_ONLY)
  91. set_list (${LIST_NAME} ENGINE_${LIST_NAME} REMOVE_DUPLICATE)
  92. endforeach ()
  93. # Add include directories to find the export header and SDL header
  94. list (APPEND INCLUDE_DIRS_ONLY ${CMAKE_CURRENT_BINARY_DIR} ../ThirdParty/SDL/include)
  95. # Setup library output path
  96. if (ANDROID)
  97. set (OUTPUT_PATH ${ANDROID_LIBRARY_OUTPUT_PATH})
  98. else ()
  99. set (OUTPUT_PATH ${PROJECT_ROOT_DIR}/${PLATFORM_PREFIX}Lib) # ${PLATFORM_PREFIX} is empty for native build
  100. endif ()
  101. set_output_directories (${OUTPUT_PATH} ARCHIVE LIBRARY)
  102. # Setup target
  103. setup_library (${URHO3D_LIB_TYPE})
  104. if (NOT ANDROID)
  105. file (READ .soversion SOVERSION)
  106. string (STRIP ${SOVERSION} SOVERSION)
  107. string (REGEX MATCH "([^.]+)\\.([^.]+)\\.(.+)" MATCHED ${SOVERSION})
  108. if (MATCHED)
  109. set_target_properties (${TARGET_NAME} PROPERTIES VERSION ${MATCHED} SOVERSION ${CMAKE_MATCH_1})
  110. else ()
  111. message (FATAL_ERROR "The .soversion file is corrupted. It should contain a version number with this format major(0xFFFF).minor(0xFF).patch-level(0xFF). e.g.: 0.1.2")
  112. endif ()
  113. endif ()
  114. install (TARGETS ${TARGET_NAME} RUNTIME DESTINATION ${DEST_RUNTIME_DIR} LIBRARY DESTINATION ${DEST_LIBRARY_DIR} ARCHIVE DESTINATION ${DEST_LIBRARY_DIR})
  115. if (NOT GIT_EXIT_CODE EQUAL 0)
  116. add_dependencies (${TARGET_NAME} ${STATIC_LIBRARY_TARGETS})
  117. endif ()
  118. # Setup dependency frameworks and libraries
  119. if (APPLE)
  120. if (IOS)
  121. setup_ios_linker_flags (LINKER_FLAGS)
  122. # Add a custom target to build Mach-O universal binary consisting of iphoneos (universal ARM archs including 'arm64' if 64-bit is enabled) and iphonesimulator (i386 arch and also x86_64 arch if 64-bit is enabled)
  123. add_custom_target (${TARGET_NAME}_universal
  124. COMMAND lipo -info $<TARGET_FILE:${TARGET_NAME}> |egrep -v 'i386.+armv7' && xcodebuild -target ${TARGET_NAME} -configuration $(CONFIGURATION) -sdk iphonesimulator && mv $<TARGET_FILE:${TARGET_NAME}>{,.iphonesimulator} && xcodebuild -target ${TARGET_NAME} -configuration $(CONFIGURATION) -sdk iphoneos && mv $<TARGET_FILE:${TARGET_NAME}>{,.iphoneos} && lipo -create -output $<TARGET_FILE:${TARGET_NAME}>{,.iphonesimulator,.iphoneos} && rm $<TARGET_FILE:${TARGET_NAME}>{.iphonesimulator,.iphoneos} || echo $<TARGET_FILE:${TARGET_NAME}> is already a Mach-O universal binary consisting of both iphoneos and iphonesimulator archs
  125. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  126. COMMENT "Creating Mach-O universal binary library consisting of both iphoneos and iphonesimulator archs")
  127. else ()
  128. # Intentionally use built-in CMAKE_EXE_LINKER_FLAGS here although CMake does not use it when building library, but the variable would be used later when configuring Urho3D.pc for MacOSX platform
  129. setup_macosx_linker_flags (CMAKE_EXE_LINKER_FLAGS)
  130. # LuaJIT 64-bit specific - replace EXE linker flags with flags for building shared library (adapted from LuaJIT's original Makefile)
  131. string (REPLACE "-pagezero_size 10000 -image_base 100000000" "-image_base 7fff04c4a000" LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
  132. endif ()
  133. set_target_properties (${TARGET_NAME} PROPERTIES LINK_FLAGS ${LINKER_FLAGS})
  134. set (FORCE_LOAD -force_load)
  135. elseif (NOT MSVC)
  136. set_property (TARGET ${TARGET_NAME} APPEND PROPERTY LINK_LIBRARIES -Wl,--whole-archive)
  137. if (WIN32 AND URHO3D_LIB_TYPE STREQUAL SHARED)
  138. set_target_properties (${TARGET_NAME} PROPERTIES PREFIX "")
  139. endif ()
  140. endif ()
  141. foreach (TARGET ${STATIC_LIBRARY_TARGETS})
  142. get_target_property (ARCHIVE ${TARGET} LOCATION)
  143. if (NOT MSVC)
  144. set_property (TARGET ${TARGET_NAME} APPEND PROPERTY LINK_LIBRARIES ${FORCE_LOAD} ${ARCHIVE}) # Only works in Apple (Xcode and Makefile) and GCC while building shared library
  145. endif ()
  146. if (XCODE OR MSVC)
  147. set_property (TARGET ${TARGET_NAME} APPEND_STRING PROPERTY STATIC_LIBRARY_FLAGS " ${ARCHIVE}") # Only works in Apple (Xcode only) and MSVC while building static library
  148. else ()
  149. list (APPEND ARCHIVES ${ARCHIVE})
  150. endif ()
  151. endforeach ()
  152. if (NOT XCODE AND NOT MSVC)
  153. if (NOT APPLE)
  154. set_property (TARGET ${TARGET_NAME} APPEND PROPERTY LINK_LIBRARIES -Wl,--no-whole-archive)
  155. endif ()
  156. if (NOT URHO3D_LIB_TYPE STREQUAL SHARED) # GCC ar does not take archives directly as input like Apple libtool, however, it can be scripted to do so
  157. if (APPLE)
  158. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  159. COMMAND mv $<TARGET_FILE:${TARGET_NAME}>{,.engine}
  160. COMMAND libtool -static $<TARGET_FILE:${TARGET_NAME}>.engine ${ARCHIVES} -o $<TARGET_FILE:${TARGET_NAME}>
  161. COMMAND rm $<TARGET_FILE:${TARGET_NAME}>.engine
  162. COMMENT "Merging all archives into a single static library using libtool")
  163. else ()
  164. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  165. COMMAND ${CMAKE_COMMAND} -E rename $<TARGET_FILE:${TARGET_NAME}> $<TARGET_FILE:${TARGET_NAME}>.engine
  166. COMMAND echo CREATE $<TARGET_FILE:${TARGET_NAME}> >script.ar
  167. COMMAND echo ADDLIB $<TARGET_FILE:${TARGET_NAME}>.engine >>script.ar)
  168. foreach (ARCHIVE ${ARCHIVES})
  169. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND echo ADDLIB ${ARCHIVE} >>script.ar)
  170. endforeach ()
  171. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  172. COMMAND echo SAVE >>script.ar
  173. COMMAND echo END >>script.ar
  174. COMMAND ${CMAKE_AR} -M <script.ar
  175. COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:${TARGET_NAME}>.engine script.ar
  176. COMMENT "Merging all archives into a single static library using ar")
  177. endif ()
  178. endif ()
  179. endif ()
  180. # todo This is a deprecated property in CMake version 2.8.12 - Remove below commands when CMake minimum version is 2.8.12
  181. if (CMAKE_VERSION VERSION_LESS 2.8.12)
  182. set_target_properties (${TARGET_NAME} PROPERTIES LINK_INTERFACE_LIBRARIES "")
  183. if (URHO3D_LIB_TYPE STREQUAL SHARED)
  184. if (NOT MSVC AND CMAKE_VERSION VERSION_LESS 2.8.11)
  185. get_target_property (LINK_LIBRARIES ${TARGET_NAME} LINK_LIBRARIES)
  186. target_link_libraries (${TARGET_NAME} LINK_PRIVATE ${LINK_LIBRARIES})
  187. endif ()
  188. else ()
  189. set_target_properties (${TARGET_NAME} PROPERTIES LINK_LIBRARIES "")
  190. endif ()
  191. endif ()
  192. # end todo
  193. # Generate platform specific export header file automatically
  194. if (NOT URHO3D_LIB_TYPE STREQUAL URHO3D_EXPORTS_LIB_TYPE OR ${CMAKE_CURRENT_SOURCE_DIR}/Urho3D.h.in IS_NEWER_THAN ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
  195. set_target_properties (${TARGET_NAME} PROPERTIES DEFINE_SYMBOL URHO3D_EXPORTS)
  196. generate_export_header (${TARGET_NAME} EXPORT_MACRO_NAME URHO3D_API EXPORT_FILE_NAME Urho3D.h)
  197. set (URHO3D_EXPORTS_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE INTERNAL "Lib type when Urho3D export header was last generated")
  198. # Append Urho3D license notice to the export header file
  199. file (READ ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h EXPORT_DEFINE)
  200. configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Urho3D.h.in ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
  201. endif ()
  202. install (FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h DESTINATION ${DEST_INCLUDE_DIR})
  203. # Generate platform specific pkg-config file for the benefit of Urho3D library users via SDK without CMake
  204. if (NOT IOS)
  205. get_directory_property (URHO3D_COMPILE_DEFINITIONS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_DEFINITIONS)
  206. list (REMOVE_ITEM URHO3D_COMPILE_DEFINITIONS HAVE_STDINT_H GLEW_STATIC GLEW_NO_GLU) # Remove those defines that are only used for building the library and not needed by library user
  207. if (ABSOLUTE_PATH_LIBS)
  208. string (REPLACE ";" "\" \"" URHO3D_ABS_PATH_LIBS "\"${ABSOLUTE_PATH_LIBS}\"") # Note: need to always "stringify" a variable in list context for replace to work correctly, besides the list could be empty
  209. string (REPLACE "${SYSROOT}" "\${pc_sysrootdir}" URHO3D_ABS_PATH_LIBS "${URHO3D_ABS_PATH_LIBS}")
  210. endif ()
  211. set (LIB_NAME Urho3D)
  212. if (MINGW)
  213. if (CMAKE_BUILD_TYPE STREQUAL Debug)
  214. set (LIB_NAME ${LIB_NAME}_d)
  215. endif ()
  216. if (URHO3D_LIB_TYPE STREQUAL SHARED)
  217. set (LIB_NAME ${LIB_NAME}.dll)
  218. endif ()
  219. endif ()
  220. if (MSVC)
  221. # todo: Add a post build command to adjust the LIB_NAME when in Debug configuration
  222. set (DASH /)
  223. set (LIB_DIR "/LIBPATH:\"\${libdir}\"")
  224. string (REPLACE ";" ".lib\" \"" URHO3D_LIBS "\"${LIB_NAME};${LINK_LIBS_ONLY}.lib\"")
  225. else ()
  226. set (DASH -)
  227. set (LIB_DIR "-L\"\${libdir}\"")
  228. string (REPLACE ";" " -l" URHO3D_LIBS "-l${LIB_NAME};${LINK_LIBS_ONLY}")
  229. endif ()
  230. string (REPLACE ";" " ${DASH}D" URHO3D_COMPILE_DEFINITIONS ";${URHO3D_COMPILE_DEFINITIONS}")
  231. get_directory_property (GLOBAL_INCLUDE_DIRS DIRECTORY ${CMAKE_SOURCE_DIR} INCLUDE_DIRECTORIES)
  232. if (GLOBAL_INCLUDE_DIRS)
  233. string (REPLACE ";" "\" ${DASH}I\"" GLOBAL_INCLUDE_DIRS "${DASH}I\"${GLOBAL_INCLUDE_DIRS}\"")
  234. string (REPLACE "${SYSROOT}" "" GLOBAL_INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS})
  235. endif ()
  236. string (REPLACE ";" "\" ${DASH}I\"\${includedir}/" ENGINE_INCLUDE_DIRS "${DASH}I\"\${includedir};${INSTALL_INCLUDE_DIRS}\"")
  237. configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Urho3D.pc.in ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc @ONLY)
  238. install (FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc DESTINATION ${DEST_PKGCONFIG_DIR})
  239. endif ()
  240. # Setup the compiler flags for building shared library (do this here so that it does not interfere with the pkg-config file generation above)
  241. if (URHO3D_LIB_TYPE STREQUAL SHARED)
  242. # Hide the symbols that are not explicitly marked for export
  243. add_compiler_export_flags ()
  244. # Use PIC on platforms that support it (shared library type has this property set to true by default, so we only have to deal with those static ones that the shared library links against)
  245. set_target_properties (${STATIC_LIBRARY_TARGETS} PROPERTIES POSITION_INDEPENDENT_CODE true)
  246. if (NOT MSVC AND NOT (CMAKE_CROSSCOMPILING AND MINGW) AND CMAKE_VERSION VERSION_LESS 2.8.9) # \todo Remove this when CMake minimum version is 2.8.9
  247. set_property (TARGET ${STATIC_LIBRARY_TARGETS} APPEND PROPERTY COMPILE_FLAGS -fPIC)
  248. endif ()
  249. endif ()
  250. # Define post build steps
  251. if (ANDROID_NDK_GDB)
  252. # Copy the library while it still has debug symbols for ndk-gdb
  253. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_NAME}> ${NDK_GDB_SOLIB_PATH}
  254. COMMENT "Copying Urho3D library with debug symbols to ${NDK_GDB_SOLIB_PATH} directory")
  255. endif ()
  256. if (URHO3D_LIB_TYPE STREQUAL SHARED AND (ANDROID OR RASPI OR IOS))
  257. # Strip the output shared library for embedded devices
  258. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${TARGET_NAME}>
  259. COMMENT "Stripping Urho3D shared library")
  260. endif ()
  261. if (URHO3D_SCP_TO_TARGET)
  262. # Ensure SCP is the last command
  263. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND scp $<TARGET_FILE:${TARGET_NAME}> ${URHO3D_SCP_TO_TARGET} || exit 0
  264. COMMENT "Scp-ing Urho3D library to target system")
  265. endif ()