Urho3D-CMake-common.cmake 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. # Set the build type if not explicitly set, for single-configuration generator only
  23. if (CMAKE_GENERATOR STREQUAL Xcode)
  24. set (XCODE TRUE)
  25. endif ()
  26. if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
  27. set (CMAKE_BUILD_TYPE Release)
  28. endif ()
  29. if (CMAKE_HOST_WIN32)
  30. execute_process (COMMAND uname -o RESULT_VARIABLE UNAME_EXIT_CODE OUTPUT_VARIABLE UNAME_OPERATING_SYSTEM ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  31. if (UNAME_EXIT_CODE EQUAL 0 AND UNAME_OPERATING_SYSTEM STREQUAL Msys)
  32. set (MSYS 1)
  33. endif ()
  34. endif ()
  35. # Define all supported build options
  36. include (CMakeDependentOption)
  37. cmake_dependent_option (IOS "Setup build for iOS platform" FALSE "XCODE" FALSE)
  38. if (NOT MSVC)
  39. # On non-MSVC compiler, default to build 64-bit when the host system has a 64-bit build environment
  40. execute_process (COMMAND echo COMMAND ${CMAKE_C_COMPILER} -E -dM - OUTPUT_VARIABLE PREDEFINED_MACROS ERROR_QUIET)
  41. string (REGEX MATCH "#define +__x86_64__ +1" matched "${PREDEFINED_MACROS}")
  42. if (NOT matched)
  43. string (REGEX MATCH "#define +__aarch64__ +1" matched "${PREDEFINED_MACROS}")
  44. endif ()
  45. if (matched)
  46. set (URHO3D_DEFAULT_64BIT TRUE)
  47. endif ()
  48. # The 'ANDROID' CMake variable is already set by android.toolchain.cmake when it is being used for cross-compiling Android
  49. # The other arm platform that Urho3D supports that is not Android is Raspberry Pi at the moment
  50. if (NOT ANDROID)
  51. string (REGEX MATCH "#define +__arm__ +1" matched "${PREDEFINED_MACROS}")
  52. if (matched)
  53. # Set the CMake variable here instead of in raspberrypi.toolchain.cmake because Raspberry Pi can be built natively too
  54. set (RASPI TRUE CACHE STRING "Setup build for Raspberry Pi platform")
  55. endif ()
  56. endif ()
  57. endif ()
  58. option (URHO3D_64BIT "Enable 64-bit build" ${URHO3D_DEFAULT_64BIT})
  59. option (URHO3D_ANGELSCRIPT "Enable AngelScript scripting support" TRUE)
  60. option (URHO3D_LUA "Enable additional Lua scripting support")
  61. option (URHO3D_LUAJIT "Enable Lua scripting support using LuaJIT (check LuaJIT's CMakeLists.txt for more options)")
  62. option (URHO3D_NAVIGATION "Enable navigation support" TRUE)
  63. option (URHO3D_NETWORK "Enable networking support" TRUE)
  64. option (URHO3D_PHYSICS "Enable physics support" TRUE)
  65. option (URHO3D_URHO2D "Enable 2D graphics and physics support" TRUE)
  66. if (MINGW AND NOT DEFINED URHO3D_SSE)
  67. # Certain MinGW versions fail to compile SSE code. This is the initial guess for known "bad" version range, and can be tightened later
  68. execute_process (COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION ERROR_QUIET)
  69. if (GCC_VERSION VERSION_LESS 4.9.1)
  70. message (WARNING "Disabling SSE by default due to MinGW version. It is recommended to upgrade to MinGW with GCC >= 4.9.1. You can also try to re-enable SSE with CMake option -DURHO3D_SSE=1, but this may result in compile errors.")
  71. else ()
  72. set (URHO3D_DEFAULT_SSE TRUE)
  73. endif ()
  74. else ()
  75. set (URHO3D_DEFAULT_SSE TRUE)
  76. endif ()
  77. option (URHO3D_SSE "Enable SSE instruction set" ${URHO3D_DEFAULT_SSE})
  78. if (CMAKE_PROJECT_NAME STREQUAL Urho3D)
  79. cmake_dependent_option (URHO3D_LUAJIT_AMALG "Enable LuaJIT amalgamated build (LuaJIT only)" FALSE "URHO3D_LUAJIT" FALSE)
  80. cmake_dependent_option (URHO3D_SAFE_LUA "Enable Lua C++ wrapper safety checks (Lua scripting only)" FALSE "URHO3D_LUA OR URHO3D_LUAJIT" FALSE)
  81. option (URHO3D_SAMPLES "Build sample applications")
  82. cmake_dependent_option (URHO3D_TOOLS "Build standalone tools (Desktop and RPI only; on Android only build Lua standalone tools)" TRUE "NOT IOS;NOT ANDROID OR URHO3D_LUA OR URHO3D_LUAJIT" FALSE)
  83. cmake_dependent_option (URHO3D_EXTRAS "Build extras (Desktop and RPI only)" FALSE "NOT IOS AND NOT ANDROID" FALSE)
  84. option (URHO3D_DOCS "Generate documentation as part of normal build")
  85. option (URHO3D_DOCS_QUIET "Generate documentation as part of normal build, suppress generation process from sending anything to stdout")
  86. cmake_dependent_option (URHO3D_MINIDUMPS "Enable minidumps on crash (VS only)" TRUE "MSVC" FALSE)
  87. option (URHO3D_FILEWATCHER "Enable filewatcher support" TRUE)
  88. endif ()
  89. option (URHO3D_PROFILING "Enable profiling support" TRUE)
  90. option (URHO3D_LOGGING "Enable logging support" TRUE)
  91. option (URHO3D_TESTING "Enable testing support")
  92. if (URHO3D_TESTING)
  93. set (URHO3D_TEST_TIME_OUT 5 CACHE STRING "Number of seconds to test run the executables")
  94. else ()
  95. unset (URHO3D_TEST_TIME_OUT CACHE)
  96. endif ()
  97. # The URHO3D_OPENGL option is not defined on non-Windows platforms as they should always use OpenGL
  98. if (MSVC)
  99. # On MSVC compiler, default to false (i.e. prefers Direct3D)
  100. # OpenGL can be manually enabled with -DURHO3D_OPENGL=1, but Windows graphics card drivers are usually better optimized for Direct3D
  101. option (URHO3D_OPENGL "Use OpenGL instead of Direct3D (Windows platform only)")
  102. elseif (WIN32)
  103. # On non-MSVC compiler on Windows platform, default to true to enable use of OpenGL instead of Direct3D
  104. # Direct3D can be manually enabled with -DURHO3D_OPENGL=0, but it is likely to fail unless the MinGW-w64 distribution is used due to dependency to Direct3D headers and libs
  105. option (URHO3D_OPENGL "Use OpenGL instead of Direct3D (Windows platform only)" TRUE)
  106. endif ()
  107. cmake_dependent_option (URHO3D_MKLINK "Use mklink command to create symbolic links (Windows Vista and above only)" FALSE "WIN32" FALSE)
  108. cmake_dependent_option (URHO3D_STATIC_RUNTIME "Use static C/C++ runtime libraries and eliminate the need for runtime DLLs installation (VS only)" FALSE "MSVC" FALSE)
  109. set (URHO3D_LIB_TYPE STATIC CACHE STRING "Specify Urho3D library type, possible values are STATIC (default) and SHARED")
  110. if (CMAKE_CROSSCOMPILING AND NOT ANDROID)
  111. set (URHO3D_SCP_TO_TARGET "" CACHE STRING "Use scp to transfer executables to target system (non-Android cross-compiling build only), SSH digital key must be setup first for this to work, typical value has a pattern of usr@tgt:remote-loc")
  112. else ()
  113. unset (URHO3D_SCP_TO_TARGET CACHE)
  114. endif ()
  115. if (ANDROID)
  116. set (ANDROID TRUE CACHE STRING "Setup build for Android platform") # Cache the CMake variable
  117. set (ANDROID_ABI armeabi-v7a CACHE STRING "Specify ABI for native code (Android build only), possible values are armeabi, armeabi-v7a (default), armeabi-v7a with NEON, armeabi-v7a with VFPV3, armeabi-v6 with VFP, arm64-v8a, x86, and x86_64")
  118. cmake_dependent_option (URHO3D_NDK_GDB "Enable ndk-gdb for debugging (Android build only)" FALSE "CMAKE_BUILD_TYPE STREQUAL Debug" FALSE)
  119. else ()
  120. unset (ANDROID_ABI CACHE)
  121. unset (URHO3D_NDK_GDB CACHE)
  122. if (ANDROID_ABI)
  123. # Just reference it to suppress "unused variable" CMake warning on non-Android project
  124. # Due to the design of cmake_gcc.sh currently, the script can be used to configure/generate Android project and other non-Android projects in one go
  125. endif ()
  126. endif ()
  127. # Enable testing
  128. if (URHO3D_TESTING)
  129. enable_testing ()
  130. add_definitions (-DURHO3D_TESTING)
  131. endif ()
  132. # Enable SSE instruction set. Requires Pentium III or Athlon XP processor at minimum.
  133. if (URHO3D_SSE)
  134. add_definitions (-DURHO3D_SSE)
  135. endif ()
  136. # Enable structured exception handling and minidumps on MSVC only.
  137. if (MSVC AND URHO3D_MINIDUMPS)
  138. add_definitions (-DURHO3D_MINIDUMPS)
  139. endif ()
  140. # By default use the MSVC dynamic runtime. To eliminate the need to distribute the runtime installer,
  141. # this can be switched off if not using Urho3D as a shared library.
  142. if (MSVC)
  143. if (URHO3D_STATIC_RUNTIME)
  144. set (RELEASE_RUNTIME /MT)
  145. set (DEBUG_RUNTIME /MTd)
  146. else ()
  147. set (RELEASE_RUNTIME "")
  148. set (DEBUG_RUNTIME "")
  149. endif ()
  150. endif ()
  151. # Enable file watcher support for automatic resource reloads by default.
  152. if (URHO3D_FILEWATCHER)
  153. add_definitions (-DURHO3D_FILEWATCHER)
  154. endif ()
  155. # Enable profiling by default. If disabled, autoprofileblocks become no-ops and the Profiler subsystem is not instantiated.
  156. if (URHO3D_PROFILING)
  157. add_definitions (-DURHO3D_PROFILING)
  158. endif ()
  159. # Enable logging by default. If disabled, LOGXXXX macros become no-ops and the Log subsystem is not instantiated.
  160. if (URHO3D_LOGGING)
  161. add_definitions (-DURHO3D_LOGGING)
  162. endif ()
  163. # If not on Windows platform, enable Unix mode for kNet library and OpenGL graphic back-end
  164. if (NOT WIN32)
  165. add_definitions (-DUNIX)
  166. set (URHO3D_OPENGL 1)
  167. endif ()
  168. # Add definition for OpenGL
  169. if (URHO3D_OPENGL)
  170. add_definitions (-DURHO3D_OPENGL)
  171. endif ()
  172. # Add definitions for GLEW
  173. if (NOT IOS AND NOT ANDROID AND NOT RASPI AND URHO3D_OPENGL)
  174. add_definitions (-DGLEW_STATIC -DGLEW_NO_GLU)
  175. endif ()
  176. # Add definition for AngelScript
  177. if (URHO3D_ANGELSCRIPT)
  178. add_definitions (-DURHO3D_ANGELSCRIPT)
  179. endif ()
  180. # Add definition for Lua and LuaJIT
  181. if (URHO3D_LUAJIT)
  182. add_definitions (-DURHO3D_LUAJIT)
  183. set (JIT JIT)
  184. # Implied URHO3D_LUA
  185. set (URHO3D_LUA 1)
  186. endif ()
  187. if (URHO3D_LUA)
  188. add_definitions (-DURHO3D_LUA)
  189. endif ()
  190. # Add definition for Navigation
  191. if (URHO3D_NAVIGATION)
  192. add_definitions (-DURHO3D_NAVIGATION)
  193. endif ()
  194. # Add definition for Network
  195. if (URHO3D_NETWORK)
  196. add_definitions (-DURHO3D_NETWORK)
  197. endif ()
  198. # Add definition for Physics
  199. if (URHO3D_PHYSICS)
  200. add_definitions (-DURHO3D_PHYSICS)
  201. endif ()
  202. # Add definition for Urho2D
  203. if (URHO3D_URHO2D)
  204. add_definitions (-DURHO3D_URHO2D)
  205. endif ()
  206. # Default library type is STATIC
  207. if (URHO3D_LIB_TYPE)
  208. string (TOUPPER ${URHO3D_LIB_TYPE} URHO3D_LIB_TYPE)
  209. endif ()
  210. if (NOT URHO3D_LIB_TYPE STREQUAL SHARED)
  211. set (URHO3D_LIB_TYPE STATIC)
  212. add_definitions (-DURHO3D_STATIC_DEFINE)
  213. endif ()
  214. # Find DirectX SDK include & library directories for Visual Studio. It is also possible to compile
  215. # without if a recent Windows SDK is installed. The SDK is not searched for with MinGW as it is
  216. # incompatible; rather, it is assumed that MinGW itself comes with the necessary headers & libraries.
  217. if (WIN32 AND NOT URHO3D_OPENGL)
  218. find_package (Direct3D)
  219. if (DIRECT3D_FOUND)
  220. include_directories (${DIRECT3D_INCLUDE_DIRS})
  221. endif ()
  222. endif ()
  223. # For Raspbery Pi, find Broadcom VideoCore IV firmware
  224. if (RASPI)
  225. find_package (BCM_VC REQUIRED)
  226. include_directories (${BCM_VC_INCLUDE_DIRS})
  227. endif ()
  228. # Platform and compiler specific options
  229. if (IOS)
  230. # IOS-specific setup
  231. add_definitions (-DIOS)
  232. if (URHO3D_64BIT)
  233. set (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_INCLUDING_64_BIT))
  234. else ()
  235. set (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
  236. endif ()
  237. set (CMAKE_XCODE_EFFECTIVE_PLATFORMS -iphoneos -iphonesimulator)
  238. if (NOT MACOSX_BUNDLE_GUI_IDENTIFIER)
  239. set (MACOSX_BUNDLE_GUI_IDENTIFIER com.github.urho3d.\${PRODUCT_NAME:rfc1034identifier})
  240. endif ()
  241. set (CMAKE_OSX_SYSROOT iphoneos) # Set Base SDK to "Latest iOS"
  242. elseif (XCODE)
  243. # MacOSX-Xcode-specific setup
  244. if (NOT URHO3D_64BIT)
  245. set (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
  246. endif ()
  247. set (CMAKE_OSX_SYSROOT macosx) # Set Base SDK to "Latest OS X"
  248. if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
  249. # If not set, set to current running build system OS version by default
  250. execute_process (COMMAND sw_vers -productVersion OUTPUT_VARIABLE CURRENT_OSX_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
  251. string (REGEX REPLACE ^\([^.]+\\.[^.]+\).* \\1 CMAKE_OSX_DEPLOYMENT_TARGET ${CURRENT_OSX_VERSION})
  252. endif ()
  253. endif ()
  254. if (MSVC)
  255. # Visual Studio-specific setup
  256. add_definitions (-D_CRT_SECURE_NO_WARNINGS)
  257. # Note: All CMAKE_xxx_FLAGS variables are not in list context (although they should be)
  258. set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DEBUG_RUNTIME}")
  259. set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE} ${RELEASE_RUNTIME} /fp:fast /Zi /GS-")
  260. set (CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELWITHDEBINFO})
  261. set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DEBUG_RUNTIME}")
  262. set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_RUNTIME} /fp:fast /Zi /GS- /D _SECURE_SCL=0")
  263. set (CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
  264. # SSE flag is redundant if already compiling as 64bit
  265. if (URHO3D_SSE AND NOT URHO3D_64BIT)
  266. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:SSE")
  267. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE")
  268. endif ()
  269. set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF /DEBUG")
  270. set (CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
  271. else ()
  272. # GCC/Clang-specific setup
  273. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof")
  274. if (ANDROID)
  275. # Most of the flags are already setup in android.toolchain.cmake module
  276. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  277. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
  278. else ()
  279. if (RASPI)
  280. add_definitions (-DRASPI)
  281. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -mcpu=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard -Wno-psabi")
  282. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe -mcpu=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard -Wno-psabi")
  283. else ()
  284. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math")
  285. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math")
  286. if (URHO3D_64BIT)
  287. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
  288. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
  289. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m64")
  290. else ()
  291. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
  292. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
  293. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
  294. if (URHO3D_SSE)
  295. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse")
  296. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse")
  297. endif ()
  298. endif ()
  299. endif ()
  300. # MinGW-specific setup
  301. if (MINGW)
  302. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static -static-libgcc -fno-keep-inline-dllexport")
  303. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -static-libstdc++ -static-libgcc -fno-keep-inline-dllexport")
  304. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static")
  305. # Additional compiler flags for Windows ports of GCC
  306. set (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
  307. set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG")
  308. # Reduce GCC optimization level from -O3 to -O2 for stability in RELEASE build type
  309. set (CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
  310. set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
  311. endif ()
  312. set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
  313. set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
  314. endif ()
  315. if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  316. if (CMAKE_GENERATOR STREQUAL Ninja)
  317. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics")
  318. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
  319. endif ()
  320. # Temporary workaround for Travis CI VM as Ubuntu 12.04 LTS still uses old glibc header files that do not have the necessary patch for Clang to work correctly
  321. # TODO: Remove this workaround when Travis CI VM has been migrated to Ubuntu 14.04 LTS (or hopefully it will be CentOS :)
  322. if (DEFINED ENV{CI} AND "$ENV{LINUX}" STREQUAL 1)
  323. add_definitions (-D__extern_always_inline=inline)
  324. endif ()
  325. endif ()
  326. endif ()
  327. # Include CMake builtin module for building shared library support
  328. include (GenerateExportHeader)
  329. # Determine the project root directory
  330. get_filename_component (PROJECT_ROOT_DIR ${PROJECT_SOURCE_DIR} PATH)
  331. # Macro for setting common output directories
  332. macro (set_output_directories OUTPUT_PATH)
  333. foreach (TYPE ${ARGN})
  334. set (CMAKE_${TYPE}_OUTPUT_DIRECTORY ${OUTPUT_PATH})
  335. foreach (CONFIG ${CMAKE_CONFIGURATION_TYPES})
  336. string (TOUPPER ${CONFIG} CONFIG)
  337. set (CMAKE_${TYPE}_OUTPUT_DIRECTORY_${CONFIG} ${OUTPUT_PATH})
  338. endforeach ()
  339. endforeach ()
  340. endmacro ()
  341. # Set common binary output directory for all targets
  342. if (IOS)
  343. set (PLATFORM_PREFIX ios-)
  344. elseif (CMAKE_CROSSCOMPILING)
  345. if (RASPI)
  346. set (PLATFORM_PREFIX raspi-)
  347. elseif (ANDROID)
  348. set (PLATFORM_PREFIX android-) # Note: this is for Android tools (ARM arch) runtime binaries, Android libs output directory is not affected by this
  349. elseif (MINGW)
  350. set (PLATFORM_PREFIX mingw-)
  351. endif ()
  352. endif ()
  353. set_output_directories (${PROJECT_ROOT_DIR}/${PLATFORM_PREFIX}Bin RUNTIME PDB)
  354. # Enable Android ndk-gdb
  355. if (URHO3D_NDK_GDB)
  356. set (NDK_GDB_SOLIB_PATH ${PROJECT_BINARY_DIR}/obj/local/${ANDROID_NDK_ABI_NAME}/)
  357. file (MAKE_DIRECTORY ${NDK_GDB_SOLIB_PATH})
  358. set (NDK_GDB_JNI ${PROJECT_BINARY_DIR}/jni)
  359. set (NDK_GDB_MK "# This is a generated file. DO NOT EDIT!\n\nAPP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
  360. foreach (MK Android.mk Application.mk)
  361. if (NOT EXISTS ${NDK_GDB_JNI}/${MK})
  362. file (WRITE ${NDK_GDB_JNI}/${MK} ${NDK_GDB_MK})
  363. endif ()
  364. endforeach ()
  365. get_directory_property (INCLUDE_DIRECTORIES DIRECTORY ${PROJECT_SOURCE_DIR} INCLUDE_DIRECTORIES)
  366. string (REPLACE ";" " " INCLUDE_DIRECTORIES "${INCLUDE_DIRECTORIES}") # Note: need to always "stringify" a variable in list context for replace to work correctly
  367. set (NDK_GDB_SETUP "# This is a generated file. DO NOT EDIT!\n\nset solib-search-path ${NDK_GDB_SOLIB_PATH}\ndirectory ${INCLUDE_DIRECTORIES}\n")
  368. file (WRITE ${ANDROID_LIBRARY_OUTPUT_PATH}/gdb.setup ${NDK_GDB_SETUP})
  369. file (COPY ${ANDROID_NDK}/prebuilt/android-${ANDROID_ARCH_NAME}/gdbserver/gdbserver DESTINATION ${ANDROID_LIBRARY_OUTPUT_PATH})
  370. endif ()
  371. # Override builtin macro and function to suit our need, always generate header file regardless of target type...
  372. macro (_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  373. set (DEFINE_DEPRECATED)
  374. set (DEFINE_EXPORT)
  375. set (DEFINE_IMPORT)
  376. set (DEFINE_NO_EXPORT)
  377. if (COMPILER_HAS_DEPRECATED_ATTR)
  378. set (DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  379. elseif (COMPILER_HAS_DEPRECATED)
  380. set (DEFINE_DEPRECATED "__declspec(deprecated)")
  381. endif ()
  382. get_property (type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  383. if (type MATCHES "STATIC|SHARED")
  384. if (WIN32)
  385. set (DEFINE_EXPORT "__declspec(dllexport)")
  386. set (DEFINE_IMPORT "__declspec(dllimport)")
  387. elseif (COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  388. set (DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  389. set (DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  390. set (DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  391. endif ()
  392. endif ()
  393. endmacro ()
  394. # ... except, when target is a module library type
  395. function (GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  396. get_property (type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  397. if (${type} MATCHES MODULE)
  398. message (WARNING "This macro should not be used with libraries of type MODULE")
  399. return ()
  400. endif ()
  401. _test_compiler_hidden_visibility ()
  402. _test_compiler_has_deprecated ()
  403. _do_set_macro_values (${TARGET_LIBRARY})
  404. _do_generate_export_header (${TARGET_LIBRARY} ${ARGN})
  405. endfunction ()
  406. # Override builtin function to suit our need, takes care of C flags as well as CXX flags
  407. function (add_compiler_export_flags)
  408. if (NOT ANDROID AND NOT MSVC AND NOT DEFINED USE_COMPILER_HIDDEN_VISIBILITY AND NOT DEFINED COMPILER_HAS_DEPRECATED)
  409. message (STATUS "Following tests check whether compiler installed in this system has export/import and deprecated attributes support")
  410. message (STATUS "CMake will generate a suitable export header file for this system based on the test result")
  411. message (STATUS "It is OK to proceed to build Urho3D regardless of the test result")
  412. endif ()
  413. _test_compiler_hidden_visibility ()
  414. _test_compiler_has_deprecated ()
  415. if (NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  416. # Just return if there are no flags to add.
  417. return ()
  418. endif ()
  419. set (EXTRA_FLAGS "-fvisibility=hidden")
  420. # Either return the extra flags needed in the supplied argument, or to the
  421. # CMAKE_C_FLAGS if no argument is supplied.
  422. if (ARGV1)
  423. set (${ARGV1} "${EXTRA_FLAGS}" PARENT_SCOPE)
  424. else ()
  425. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  426. endif ()
  427. if (COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  428. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  429. endif ()
  430. # Either return the extra flags needed in the supplied argument, or to the
  431. # CMAKE_CXX_FLAGS if no argument is supplied.
  432. if (ARGV0)
  433. set (${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  434. else ()
  435. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  436. endif ()
  437. endfunction ()
  438. # Macro for precompiled headers
  439. macro (enable_pch)
  440. if (MSVC)
  441. foreach (FILE ${SOURCE_FILES})
  442. if (FILE MATCHES \\.cpp$)
  443. if (FILE MATCHES Precompiled\\.cpp$)
  444. set_source_files_properties (${FILE} PROPERTIES COMPILE_FLAGS "/YcPrecompiled.h")
  445. else ()
  446. set_source_files_properties (${FILE} PROPERTIES COMPILE_FLAGS "/YuPrecompiled.h")
  447. endif ()
  448. endif ()
  449. endforeach ()
  450. else ()
  451. # TODO: to enable usage of precompiled header in GCC, for now just make sure the correct Precompiled.h is found in the search
  452. foreach (FILE ${SOURCE_FILES})
  453. if (FILE MATCHES Precompiled\\.h$)
  454. get_filename_component (PATH ${FILE} PATH)
  455. include_directories (${PATH})
  456. break ()
  457. endif ()
  458. endforeach ()
  459. endif ()
  460. endmacro ()
  461. # Macro for setting up dependency lib for compilation and linking of a target
  462. macro (setup_target)
  463. # Include directories
  464. include_directories (${LIBS} ${INCLUDE_DIRS_ONLY})
  465. # Link libraries
  466. define_dependency_libs (${TARGET_NAME})
  467. string (REGEX REPLACE \\.\\./|ThirdParty/|Engine/|Extras/|/include|/src "" STRIP_LIBS "${LIBS};${LINK_LIBS_ONLY}")
  468. target_link_libraries (${TARGET_NAME} ${ABSOLUTE_PATH_LIBS} ${STRIP_LIBS})
  469. # CMake does not support IPHONEOS_DEPLOYMENT_TARGET the same manner as it supports CMAKE_OSX_DEPLOYMENT_TARGET
  470. # The iOS deployment target is set using the corresponding Xcode attribute as target property instead
  471. if (IOS AND IPHONEOS_DEPLOYMENT_TARGET)
  472. set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${IPHONEOS_DEPLOYMENT_TARGET})
  473. endif ()
  474. # Workaround CMake/Xcode generator bug where it always appends '/build' path element to SYMROOT attribute and as such the items in Products are always rendered as red as if they are not yet built
  475. if (XCODE)
  476. file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/build)
  477. get_target_property (LOCATION ${TARGET_NAME} LOCATION)
  478. string (REGEX REPLACE "^.*\\$\\(CONFIGURATION\\)" $(CONFIGURATION) SYMLINK ${LOCATION})
  479. get_filename_component (DIRECTORY ${SYMLINK} PATH)
  480. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  481. COMMAND mkdir -p ${DIRECTORY} && ln -s -f $<TARGET_FILE:${TARGET_NAME}> ${DIRECTORY}/$<TARGET_FILE_NAME:${TARGET_NAME}>
  482. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/build)
  483. endif ()
  484. endmacro ()
  485. # Macro for checking the SOURCE_FILES variable is properly initialized
  486. macro (check_source_files)
  487. if (NOT SOURCE_FILES)
  488. message (FATAL_ERROR "Could not configure and generate the project file because no source files have been defined yet. "
  489. "You can define the source files explicitly by setting the SOURCE_FILES variable in your CMakeLists.txt; or "
  490. "by calling the define_source_files() macro which would by default glob all the C++ source files found in the same scope of "
  491. "CMakeLists.txt where the macro is being called and the macro would set the SOURCE_FILES variable automatically. "
  492. "If your source files are not located in the same directory as the CMakeLists.txt or your source files are "
  493. "more than just C++ language then you probably have to pass in extra arguments when calling the macro in order to make it works. "
  494. "See the define_source_files() macro definition in the Source/CMake/Modules/Urho3D-CMake-common.cmake for more detail.")
  495. endif ()
  496. endmacro ()
  497. # Macro for setting up a library target
  498. macro (setup_library)
  499. check_source_files ()
  500. add_library (${TARGET_NAME} ${ARGN} ${SOURCE_FILES})
  501. setup_target ()
  502. if (CMAKE_PROJECT_NAME STREQUAL Urho3D)
  503. if (NOT ${TARGET_NAME} STREQUAL Urho3D)
  504. # Only interested in static library type, i.e. exclude shared and module library types
  505. get_target_property (LIB_TYPE ${TARGET_NAME} TYPE)
  506. if (LIB_TYPE MATCHES STATIC)
  507. set (STATIC_LIBRARY_TARGETS ${STATIC_LIBRARY_TARGETS} ${TARGET_NAME} PARENT_SCOPE)
  508. endif ()
  509. endif ()
  510. if (URHO3D_LIB_TYPE STREQUAL SHARED)
  511. set_target_properties (${TARGET_NAME} PROPERTIES COMPILE_DEFINITIONS URHO3D_EXPORTS)
  512. endif ()
  513. elseif (URHO3D_SCP_TO_TARGET)
  514. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND scp $<TARGET_FILE:${TARGET_NAME}> ${URHO3D_SCP_TO_TARGET} || exit 0
  515. COMMENT "Scp-ing ${TARGET_NAME} library to target system")
  516. endif ()
  517. endmacro ()
  518. # Macro for setting up an executable target
  519. # NODEPS - setup executable target without defining Urho3D dependency libraries
  520. # WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable command
  521. macro (setup_executable)
  522. # Parse extra arguments
  523. cmake_parse_arguments (ARG "NODEPS" "" "" ${ARGN})
  524. check_source_files ()
  525. add_executable (${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES})
  526. if (ARG_NODEPS)
  527. define_dependency_libs (Urho3D-nodeps)
  528. else ()
  529. define_dependency_libs (Urho3D)
  530. endif ()
  531. setup_target ()
  532. if (IOS)
  533. set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2")
  534. elseif (URHO3D_SCP_TO_TARGET)
  535. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD COMMAND scp $<TARGET_FILE:${TARGET_NAME}> ${URHO3D_SCP_TO_TARGET} || exit 0
  536. COMMENT "Scp-ing ${TARGET_NAME} executable to target system")
  537. endif ()
  538. if (DEST_RUNTIME_DIR)
  539. # Need to check if the variable is defined first because this macro could be called by CMake project outside of Urho3D that does not wish to install anything
  540. install (TARGETS ${TARGET_NAME} RUNTIME DESTINATION ${DEST_RUNTIME_DIR} BUNDLE DESTINATION ${DEST_RUNTIME_DIR})
  541. endif ()
  542. endmacro ()
  543. # Macro for setting up linker flags for Mac OS X desktop build
  544. macro (setup_macosx_linker_flags LINKER_FLAGS)
  545. set (${LINKER_FLAGS} "${${LINKER_FLAGS}} -framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework ForceFeedback -framework IOKit -framework OpenGL -framework CoreServices")
  546. endmacro ()
  547. # Macro for setting up linker flags for IOS build
  548. macro (setup_ios_linker_flags LINKER_FLAGS)
  549. set (${LINKER_FLAGS} "${${LINKER_FLAGS}} -framework AudioToolbox -framework CoreAudio -framework CoreGraphics -framework Foundation -framework OpenGLES -framework QuartzCore -framework UIKit")
  550. endmacro ()
  551. # Macro for adding SDL native init function on Android platform
  552. macro (add_android_native_init)
  553. # This source file could not be added when building SDL static library because it needs SDL_Main() which is not yet available at library building time
  554. # The SDL_Main() is defined by Android application that could be resided in other CMake projects outside of Urho3D CMake project which makes things a little bit complicated
  555. if (URHO3D_HOME)
  556. # Search using project source directory which for sure is not rooted
  557. find_file (ANDROID_MAIN_C_PATH SDL_android_main.c PATHS ${URHO3D_HOME}/Source/ThirdParty/SDL/src/main/android DOC "Path to SDL_android_main.c" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  558. else ()
  559. # Search using Urho3D SDK installation location which could be rooted
  560. find_file (ANDROID_MAIN_C_PATH SDL_android_main.c PATH_SUFFIXES ${PATH_SUFFIX} DOC "Path to SDL_android_main.c")
  561. endif ()
  562. if (ANDROID_MAIN_C_PATH)
  563. list (APPEND SOURCE_FILES ${ANDROID_MAIN_C_PATH})
  564. else ()
  565. message (FATAL_ERROR
  566. "Could not find SDL_android_main.c source file in default SDK installation location or Urho3D project root tree. "
  567. "For searching in a non-default Urho3D SDK installation, use 'CMAKE_PREFIX_PATH' environment variable to specify the prefix path of the installation location. "
  568. "For searching in a source tree of Urho3D project, use 'URHO3D_HOME' environment variable to specify the Urho3D project root directory.")
  569. endif ()
  570. endmacro ()
  571. # Macro for setting up an executable target with resources to copy
  572. macro (setup_main_executable)
  573. # Define resource files
  574. if (XCODE)
  575. set (RESOURCE_FILES ${PROJECT_ROOT_DIR}/Bin/CoreData ${PROJECT_ROOT_DIR}/Bin/Data)
  576. set_source_files_properties (${RESOURCE_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  577. list (APPEND SOURCE_FILES ${RESOURCE_FILES})
  578. endif ()
  579. if (ANDROID)
  580. # Add SDL native init function, SDL_Main() entry point must be defined by one of the source files in ${SOURCE_FILES}
  581. add_android_native_init ()
  582. # Setup shared library output path
  583. set_output_directories (${ANDROID_LIBRARY_OUTPUT_PATH} LIBRARY)
  584. # Setup target as main shared library
  585. define_dependency_libs (Urho3D)
  586. setup_library (SHARED)
  587. # Copy other dependent shared libraries to Android library output path
  588. foreach (FILE ${ABSOLUTE_PATH_LIBS})
  589. get_filename_component (EXT ${FILE} EXT)
  590. if (EXT STREQUAL .so)
  591. get_filename_component (NAME ${FILE} NAME)
  592. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  593. COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${FILE} ${ANDROID_LIBRARY_OUTPUT_PATH}
  594. COMMENT "Copying ${NAME} to library output directory")
  595. endif ()
  596. endforeach ()
  597. if (URHO3D_NDK_GDB)
  598. # Copy the library while it still has debug symbols for ndk-gdb
  599. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  600. COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_NAME}> ${NDK_GDB_SOLIB_PATH}
  601. COMMENT "Copying lib${TARGET_NAME}.so with debug symbols to ${NDK_GDB_SOLIB_PATH} directory")
  602. endif ()
  603. # Strip target main shared library
  604. add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
  605. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${TARGET_NAME}>
  606. COMMENT "Stripping lib${TARGET_NAME}.so in library output directory")
  607. # When performing packaging, include the final apk file
  608. if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND NOT APK_INCLUDED)
  609. install (FILES ${LIBRARY_OUTPUT_PATH_ROOT}/bin/Urho3D-debug.apk DESTINATION ${DEST_RUNTIME_DIR} OPTIONAL)
  610. set (APK_INCLUDED 1)
  611. endif ()
  612. else ()
  613. # Setup target as executable
  614. if (WIN32)
  615. set (EXE_TYPE WIN32)
  616. elseif (IOS)
  617. set (EXE_TYPE MACOSX_BUNDLE)
  618. setup_ios_linker_flags (CMAKE_EXE_LINKER_FLAGS)
  619. elseif (APPLE)
  620. setup_macosx_linker_flags (CMAKE_EXE_LINKER_FLAGS)
  621. endif ()
  622. setup_executable (${EXE_TYPE})
  623. if (WIN32)
  624. set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX _d)
  625. endif ()
  626. endif ()
  627. if (IOS)
  628. get_target_property (TARGET_LOC ${TARGET_NAME} LOCATION)
  629. # Define a custom target to check for resource modification
  630. string (REGEX REPLACE /Contents/MacOS "" TARGET_LOC ${TARGET_LOC}) # The regex replacement is temporary workaround to correct the wrong location caused by CMake/Xcode generator bug
  631. add_custom_target (RESOURCE_CHECK_${TARGET_NAME} ALL
  632. \(\( `find ${RESOURCE_FILES} -newer ${TARGET_LOC} 2>/dev/null |wc -l` \)\) && touch -cm ${SOURCE_FILES} || exit 0
  633. COMMENT "Checking for changes in the Resource folders")
  634. add_dependencies (${TARGET_NAME} RESOURCE_CHECK_${TARGET_NAME})
  635. endif ()
  636. endmacro ()
  637. # Macro for adjusting target output name by dropping _suffix from the target name
  638. macro (adjust_target_name)
  639. string (REGEX REPLACE _.*$ "" OUTPUT_NAME ${TARGET_NAME})
  640. set_target_properties (${TARGET_NAME} PROPERTIES OUTPUT_NAME ${OUTPUT_NAME})
  641. endmacro ()
  642. # Macro for defining external library dependencies
  643. # The purpose of this macro is emulate CMake to set the external library dependencies transitively
  644. # It works for both targets setup within Urho3D project and outside Urho3D project that uses Urho3D as external static/shared library
  645. macro (define_dependency_libs TARGET)
  646. # ThirdParty/SDL external dependency
  647. if (${TARGET} MATCHES SDL|Urho3D)
  648. if (WIN32)
  649. list (APPEND LINK_LIBS_ONLY user32 gdi32 winmm imm32 ole32 oleaut32 version uuid)
  650. elseif (APPLE)
  651. list (APPEND LINK_LIBS_ONLY dl pthread)
  652. elseif (ANDROID)
  653. list (APPEND LINK_LIBS_ONLY dl log android)
  654. else ()
  655. # Linux
  656. list (APPEND LINK_LIBS_ONLY dl pthread rt)
  657. if (RASPI)
  658. list (APPEND ABSOLUTE_PATH_LIBS ${BCM_VC_LIBRARIES})
  659. endif ()
  660. endif ()
  661. endif ()
  662. # ThirdParty/kNet & ThirdParty/Civetweb external dependency
  663. if (${TARGET} MATCHES Civetweb|kNet|Urho3D)
  664. if (WIN32)
  665. list (APPEND LINK_LIBS_ONLY ws2_32)
  666. elseif (NOT ANDROID)
  667. list (APPEND LINK_LIBS_ONLY pthread)
  668. endif ()
  669. endif ()
  670. # Engine/LuaJIT external dependency
  671. if (URHO3D_LUAJIT AND ${TARGET} MATCHES LuaJIT|Urho3D)
  672. if (NOT WIN32)
  673. list (APPEND LINK_LIBS_ONLY dl m)
  674. endif ()
  675. endif ()
  676. # Engine external dependency
  677. if (${TARGET} STREQUAL Urho3D)
  678. # Core
  679. if (WIN32)
  680. list (APPEND LINK_LIBS_ONLY winmm)
  681. if (URHO3D_MINIDUMPS)
  682. list (APPEND LINK_LIBS_ONLY dbghelp)
  683. endif ()
  684. elseif (NOT ANDROID)
  685. list (APPEND LINK_LIBS_ONLY pthread)
  686. endif ()
  687. # Graphics
  688. if (URHO3D_OPENGL)
  689. if (WIN32)
  690. list (APPEND LINK_LIBS_ONLY opengl32)
  691. elseif (ANDROID)
  692. list (APPEND LINK_LIBS_ONLY GLESv1_CM GLESv2)
  693. elseif (NOT APPLE AND NOT RASPI)
  694. list (APPEND LINK_LIBS_ONLY GL)
  695. endif ()
  696. else ()
  697. if (DIRECT3D_FOUND)
  698. list (APPEND ABSOLUTE_PATH_LIBS ${DIRECT3D_LIBRARIES})
  699. else ()
  700. # If SDK not found, assume the libraries are found from default directories
  701. list (APPEND LINK_LIBS_ONLY ${DIRECT3D_LIBRARIES})
  702. endif ()
  703. endif ()
  704. # This variable value can either be 'Urho3D' target or an absolute path to an actual static/shared Urho3D library or empty (if we are building the library itself)
  705. # The former would cause CMake not only to link against the Urho3D library but also to add a dependency to Urho3D target
  706. if (URHO3D_LIBRARIES)
  707. if (WIN32 AND URHO3D_LIBRARIES_DBG AND URHO3D_LIBRARIES_REL AND TARGET ${TARGET_NAME})
  708. # Special handling when both debug and release libraries are being found
  709. target_link_libraries (${TARGET_NAME} debug ${URHO3D_LIBRARIES_DBG} optimized ${URHO3D_LIBRARIES_REL})
  710. else ()
  711. list (APPEND ABSOLUTE_PATH_LIBS ${URHO3D_LIBRARIES})
  712. endif ()
  713. endif ()
  714. endif ()
  715. # LuaJIT specific - extra linker flags for linking against LuaJIT (adapted from LuaJIT's original Makefile)
  716. if (URHO3D_LUAJIT AND ${TARGET} MATCHES Urho3D)
  717. # 64-bit Mac OS X
  718. if (URHO3D_64BIT AND APPLE AND NOT IOS)
  719. set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pagezero_size 10000 -image_base 100000000")
  720. endif ()
  721. # GCC-specific
  722. if (NOT WIN32 AND NOT APPLE AND NOT ANDROID)
  723. set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-E")
  724. endif ()
  725. endif ()
  726. endmacro ()
  727. # Macro for sorting and removing duplicate values
  728. macro (remove_duplicate LIST_NAME)
  729. if (${LIST_NAME})
  730. list (SORT ${LIST_NAME})
  731. list (REMOVE_DUPLICATES ${LIST_NAME})
  732. endif ()
  733. endmacro ()
  734. # Macro for setting a list from another with option to sort and remove duplicate values
  735. macro (set_list TO_LIST FROM_LIST)
  736. set (${TO_LIST} ${${FROM_LIST}})
  737. if (${ARGN} STREQUAL REMOVE_DUPLICATE)
  738. remove_duplicate (${TO_LIST})
  739. endif ()
  740. endmacro ()
  741. # Macro for defining source files with optional arguments as follows:
  742. # GROUP <value> - Group source files into a sub-group folder in VS and Xcode (only works in curent scope context)
  743. # GLOB_CPP_PATTERNS <list> - Use the provided globbing patterns for CPP_FILES instead of the default *.cpp
  744. # GLOB_H_PATTERNS <list> - Use the provided globbing patterns for H_FILES instead of the default *.h
  745. # EXTRA_CPP_FILES <list> - Include the provided list of files into CPP_FILES result
  746. # EXTRA_H_FILES <list> - Include the provided list of files into H_FILES result
  747. # PCH - Enable precompiled header on the defined source files
  748. # PARENT_SCOPE - Glob source files in current directory but set the result in parent-scope's variable ${DIR}_CPP_FILES and ${DIR}_H_FILES instead
  749. macro (define_source_files)
  750. # Parse extra arguments
  751. cmake_parse_arguments (ARG "PCH;PARENT_SCOPE" "GROUP" "EXTRA_CPP_FILES;EXTRA_H_FILES;GLOB_CPP_PATTERNS;GLOB_H_PATTERNS" ${ARGN})
  752. # Source files are defined by globbing source files in current source directory and also by including the extra source files if provided
  753. if (NOT ARG_GLOB_CPP_PATTERNS)
  754. set (ARG_GLOB_CPP_PATTERNS *.cpp) # Default glob pattern
  755. endif ()
  756. if (NOT ARG_GLOB_H_PATTERNS)
  757. set (ARG_GLOB_H_PATTERNS *.h)
  758. endif ()
  759. file (GLOB CPP_FILES ${ARG_GLOB_CPP_PATTERNS})
  760. file (GLOB H_FILES ${ARG_GLOB_H_PATTERNS})
  761. list (APPEND CPP_FILES ${ARG_EXTRA_CPP_FILES})
  762. list (APPEND H_FILES ${ARG_EXTRA_H_FILES})
  763. set (SOURCE_FILES ${CPP_FILES} ${H_FILES})
  764. # Optionally enable PCH
  765. if (ARG_PCH)
  766. enable_pch ()
  767. endif ()
  768. # Optionally accumulate source files at parent scope
  769. if (ARG_PARENT_SCOPE)
  770. get_filename_component (DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  771. set (${DIR_NAME}_CPP_FILES ${CPP_FILES} PARENT_SCOPE)
  772. set (${DIR_NAME}_H_FILES ${H_FILES} PARENT_SCOPE)
  773. # Optionally put source files into further sub-group (only works for current scope due to CMake limitation)
  774. elseif (ARG_GROUP)
  775. source_group ("Source Files\\${ARG_GROUP}" FILES ${CPP_FILES})
  776. source_group ("Header Files\\${ARG_GROUP}" FILES ${H_FILES})
  777. endif ()
  778. endmacro ()