CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # root CMakeLists for the LaundredSheep project
  2. project(LaundredSheep)
  3. set(PROJECT_VERSION "0.0.1")
  4. cmake_minimum_required(VERSION 2.8.4)
  5. if(NOT (CMAKE_MAJOR_VERSION VERSION_LESS 3))
  6. cmake_policy(SET CMP0043 OLD)
  7. endif()
  8. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
  9. include(BuildTypeLSRelease)
  10. if (NOT CMAKE_BUILD_TYPE)
  11. message(STATUS "No build type selected, default to LSRelease")
  12. set(CMAKE_BUILD_TYPE "LSRelease")
  13. endif()
  14. option(CHECK_ASSETS "Check if assets are installed in ../ls-assets" ON)
  15. if(UNIX AND NOT APPLE)
  16. option(USE_XRANDR "Use xrandr instead of vidmode" ON)
  17. option(USE_ASAN "Build with Leak/Address sanitizer" OFF)
  18. endif()
  19. set(LS_SOURCE_DIR "src")
  20. set(LS_DATA_DIR "${PROJECT_SOURCE_DIR}/data")
  21. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
  22. # Define install paths
  23. set(LS_INSTALL_BINARY_DIR "bin" CACHE
  24. STRING "Install executable to this directory, absolute or relative to CMAKE_INSTALL_PREFIX")
  25. set(LS_INSTALL_DATA_DIR "share/laundredsheep" CACHE
  26. STRING "Install data folder to this directory, absolute or relative to CMAKE_INSTALL_PREFIX")
  27. # These variables enable MSVC to find libraries located in "dependencies"
  28. if(WIN32)
  29. set(ENV{PATH} "$ENV{PATH};${PROJECT_SOURCE_DIR}/dependencies/include")
  30. set(ENV{LIB} ${PROJECT_SOURCE_DIR}/dependencies/lib)
  31. set(ENV{OPENALDIR} ${PROJECT_SOURCE_DIR}/dependencies)
  32. add_definitions(-D_IRR_STATIC_LIB_)
  33. endif()
  34. # Build glew library
  35. add_subdirectory("${PROJECT_SOURCE_DIR}/lib/glew")
  36. include_directories("${PROJECT_SOURCE_DIR}/lib/glew/include")
  37. if((WIN32 AND NOT MINGW) OR APPLE)
  38. if (NOT APPLE)
  39. # Build zlib library
  40. add_subdirectory("${PROJECT_SOURCE_DIR}/lib/zlib")
  41. include_directories("${PROJECT_SOURCE_DIR}/lib/zlib")
  42. set(ZLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lib/zlib" "${PROJECT_BINARY_DIR}/lib/zlib/")
  43. set(ZLIB_LIBRARY zlibstatic)
  44. endif()
  45. # Build png library
  46. set (SKIP_INSTALL_ALL TRUE)
  47. set (PNG_STATIC TRUE)
  48. set (PNG_TESTS FALSE)
  49. set (PNG_SHARED FALSE)
  50. add_subdirectory("${PROJECT_SOURCE_DIR}/lib/libpng")
  51. include_directories("${PROJECT_SOURCE_DIR}/lib/libpng")
  52. #build jpeg library
  53. add_subdirectory("${PROJECT_SOURCE_DIR}/lib/jpeglib")
  54. include_directories("${PROJECT_SOURCE_DIR}/lib/jpeglib")
  55. set(PNG_PNG_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lib/libpng/")
  56. set(PNG_LIBRARY png15_static)
  57. set(JPEG_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lib/jpeglib/")
  58. set(JPEG_LIBRARY jpeglib)
  59. endif()
  60. # Build the irrlicht library
  61. add_subdirectory("${PROJECT_SOURCE_DIR}/lib/irrlicht")
  62. include_directories("${PROJECT_SOURCE_DIR}/lib/irrlicht/include")
  63. # Set include paths
  64. include_directories(${LS_SOURCE_DIR})
  65. if(APPLE)
  66. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch i386")
  67. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch i386 -F/Library/Frameworks")
  68. elseif(MSVC)
  69. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") # Enable multi-processor compilation (faster)
  70. endif()
  71. # OpenMP
  72. find_package(OpenMP)
  73. if (OPENMP_FOUND)
  74. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  75. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
  76. endif()
  77. # OpenGL
  78. find_package(OpenGL REQUIRED)
  79. include_directories(${OPENGL_INCLUDE_DIR})
  80. if(UNIX AND NOT APPLE)
  81. find_package(X11 REQUIRED)
  82. include_directories(${X11_INCLUDE_DIR})
  83. if(USE_XRANDR)
  84. find_package(Xrandr REQUIRED)
  85. if(NOT XRANDR_FOUND)
  86. message(FATAL_ERROR "XRANDR not found.")
  87. endif()
  88. else()
  89. find_library(IRRLICHT_XF86VM_LIBRARY Xxf86vm)
  90. mark_as_advanced(IRRLICHT_XF86VM_LIBRARY)
  91. endif()
  92. endif()
  93. # Set some compiler options
  94. if(UNIX OR MINGW)
  95. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
  96. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  97. endif()
  98. if(MINGW AND CMAKE_BUILD_TYPE MATCHES Release)
  99. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--subsystem,windows")
  100. endif()
  101. if(WIN32)
  102. # By default windows.h has macros defined for min and max that screw up everything
  103. add_definitions(-DNOMINMAX)
  104. # And shut up about unsafe stuff
  105. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  106. endif()
  107. if(MSVC)
  108. # VS will automatically add NDEBUG for release mode, but only _DEBUG in debug mode.
  109. # Since LS uses DEBUG, this is added for debug compilation only:
  110. set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS_DEBUG DEBUG)
  111. else()
  112. # All non VS generators used create only a single compile mode, so
  113. # compile flags can be simplye be added
  114. if(CMAKE_BUILD_TYPE MATCHES Debug)
  115. add_definitions(-DDEBUG)
  116. else()
  117. add_definitions(-DNDEBUG)
  118. endif()
  119. endif()
  120. # TODO: remove this switch
  121. add_definitions(-DHAVE_OGGVORBIS)
  122. #if(WIN32)
  123. # configure_file("${LS_SOURCE_DIR}/windows_installer/icon_rc.template" "${PROJECT_BINARY_DIR}/tmp/icon.rc")
  124. #endif()
  125. # Provides list of source and header files (LS_SOURCES and LS_HEADERS)
  126. include(sources.cmake)
  127. # Generate source groups useful for MSVC project explorer
  128. include(cmake/SourceGroupFunctions.cmake)
  129. source_group_hierarchy(LS_SOURCES LS_HEADERS)
  130. if(APPLE)
  131. # icon files to copy in the bundle
  132. set(OSX_ICON_FILES ${PROJECT_SOURCE_DIR}/data/laundredsheep.icns)
  133. set_source_files_properties(${OSX_ICON_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  134. set(LS_SOURCES ${LS_SOURCES} ${OSX_ICON_FILES})
  135. # build the executable and create an app bundle
  136. add_executable(laundredsheep MACOSX_BUNDLE ${LS_SOURCES})
  137. find_library(IOKIT_LIBRARY IOKit)
  138. find_library(QUICKTIME_LIBRARY QuickTime)
  139. find_library(CARBON_LIBRARY Carbon)
  140. find_library(AUDIOUNIT_LIBRARY AudioUnit)
  141. find_library(COCOA_LIBRARY Cocoa)
  142. target_link_libraries(laundredsheep
  143. ${IOKIT_LIBRARY}
  144. ${QUICKTIME_LIBRARY}
  145. ${CARBON_LIBRARY}
  146. ${AUDIOUNIT_LIBRARY}
  147. ${COCOA_LIBRARY})
  148. # configure CMake to use a custom Info.plist
  149. set_target_properties(laundredsheep PROPERTIES
  150. MACOSX_BUNDLE_INFO_PLIST ${PROJECT_SOURCE_DIR}/data/LaundredSheep-Info.plist)
  151. if(CMAKE_GENERATOR MATCHES "Xcode")
  152. add_custom_command(TARGET laundredsheep POST_BUILD
  153. COMMAND ln -f -s ${PROJECT_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/bin/\${CONFIGURATION}/laundredsheep.app/Contents/Resources)
  154. else()
  155. add_custom_command(TARGET laundredsheep POST_BUILD
  156. COMMAND ln -f -s ${PROJECT_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/bin/laundredsheep.app/Contents/Resources)
  157. endif()
  158. else()
  159. if(MSVC)
  160. set(PTHREAD_NAMES pthreadVC2)
  161. elseif(MINGW)
  162. set(PTHREAD_NAMES "winpthread-1" "libwinpthread-1" "pthreadGC2")
  163. endif()
  164. find_library(PTHREAD_LIBRARY NAMES pthread ${PTHREAD_NAMES} PATHS ${PROJECT_SOURCE_DIR}/dependencies/lib)
  165. mark_as_advanced(PTHREAD_LIBRARY)
  166. # Set data dir (absolute or relative to CMAKE_INSTALL_PREFIX)
  167. if(NOT LS_INSTALL_DATA_DIR_ABSOLUTE)
  168. get_filename_component(LS_INSTALL_DATA_DIR_ABSOLUTE ${LS_INSTALL_DATA_DIR} ABSOLUTE)
  169. if(${LS_INSTALL_DATA_DIR_ABSOLUTE} STREQUAL ${LS_INSTALL_DATA_DIR})
  170. add_definitions(-DLAUNDREDSHEEP_DATADIR=\"${LS_INSTALL_DATA_DIR_ABSOLUTE}\")
  171. else()
  172. add_definitions(-DLAUNDREDSHEEP_DATADIR=\"${CMAKE_INSTALL_PREFIX}/${LS_INSTALL_DATA_DIR}\")
  173. endif()
  174. else()
  175. add_definitions(-DLAUNDREDSHEEP_DATADIR=\"${LS_INSTALL_DATA_DIR_ABSOLUTE}\")
  176. endif()
  177. # Build the final executable
  178. add_executable(laundredsheep ${LS_SOURCES} ${LS_RESOURCES} ${LS_HEADERS})
  179. target_link_libraries(laundredsheep ${PTHREAD_LIBRARY})
  180. endif()
  181. # CURL
  182. if(WIN32)
  183. target_link_libraries(laundredsheep ${PROJECT_SOURCE_DIR}/dependencies/lib/libcurldll.a)
  184. else()
  185. find_package(CURL REQUIRED)
  186. include_directories(${CURL_INCLUDE_DIRS})
  187. endif()
  188. # Common library dependencies
  189. target_link_libraries(laundredsheep
  190. glew
  191. lsirrlicht
  192. ${CURL_LIBRARIES}
  193. ${OGGVORBIS_LIBRARIES}
  194. ${OPENAL_LIBRARY}
  195. ${OPENGL_LIBRARIES}
  196. )
  197. if(UNIX AND NOT APPLE)
  198. target_link_libraries(laundredsheep ${X11_LIBRARIES})
  199. if(USE_XRANDR)
  200. target_link_libraries(laundredsheep ${XRANDR_LIBRARIES})
  201. else()
  202. target_link_libraries(laundredsheep ${IRRLICHT_XF86VM_LIBRARY})
  203. endif()
  204. if(USE_ASAN)
  205. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
  206. target_link_libraries(laundredsheep "-fsanitize=address")
  207. endif()
  208. endif()
  209. if(APPLE)
  210. # In theory it would be cleaner to let CMake detect the right dependencies. In practice, this means that if a OSX user has
  211. # unix-style installs of Vorbis/Ogg/OpenAL/etc. they will be picked up over our frameworks. This is blocking when I make releases :
  212. # the mac I use to make LS releases does have other installs of vorbis/ogg/etc. which aren't compatible with LS, so letting
  213. # CMake pick the library it wants essentially means I can't build.
  214. set_target_properties(laundredsheep PROPERTIES LINK_FLAGS "-arch i386 -F/Library/Frameworks -framework OpenAL -framework Ogg -framework Vorbis")
  215. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/Library/Frameworks/OpenAL.framework/Versions/A/Headers")
  216. endif()
  217. if(MSVC OR MINGW)
  218. target_link_libraries(laundredsheep iphlpapi.lib)
  219. add_custom_command(TARGET laundredsheep POST_BUILD
  220. COMMAND ${CMAKE_COMMAND} -E copy_directory
  221. "${PROJECT_SOURCE_DIR}/dependencies/dll"
  222. $<TARGET_FILE_DIR:laundredsheep>)
  223. add_custom_target(lsshaders SOURCES ${LS_SHADERS})
  224. endif()
  225. if(MINGW)
  226. find_library(LIBGCC NAMES "libgcc_s_dw2-1.dll" "libgcc_s_sjlj-1.dll" PATHS ${CMAKE_FIND_ROOT_PATH})
  227. if(LIBGCC)
  228. file(COPY ${LIBGCC} DESTINATION ${CMAKE_BINARY_DIR}/bin/)
  229. endif()
  230. find_library(LIBSTDCPP NAMES "libstdc++-6.dll" PATHS ${CMAKE_FIND_ROOT_PATH})
  231. if(LIBSTDCPP)
  232. file(COPY ${LIBSTDCPP} DESTINATION ${CMAKE_BINARY_DIR}/bin/)
  233. endif()
  234. find_library(LIBOPENMP NAMES "libgomp-1.dll" PATHS ${CMAKE_FIND_ROOT_PATH})
  235. if(LIBOPENMP)
  236. file(COPY ${LIBOPENMP} DESTINATION ${CMAKE_BINARY_DIR}/bin/)
  237. endif()
  238. find_library(LIBPTHREAD NAMES "winpthread-1.dll" "libwinpthread-1.dll" "pthreadGC2.dll" PATHS ${CMAKE_FIND_ROOT_PATH})
  239. if(LIBPTHREAD)
  240. file(COPY ${LIBPTHREAD} DESTINATION ${CMAKE_BINARY_DIR}/bin/)
  241. endif()
  242. endif()
  243. # Optional tools
  244. #add_subdirectory(tools/font_tool)
  245. # ==== Make dist target ====
  246. if(MSVC OR MINGW)
  247. # Don't create a dist target for VS
  248. else()
  249. add_custom_target(dist
  250. COMMAND rm -rf ${CMAKE_BINARY_DIR}/LaundredSheep-${PROJECT_VERSION} && rm -f ${CMAKE_BINARY_DIR}/LaundredSheep-${PROJECT_VERSION}.tar.bz2
  251. && echo "Exporting..."
  252. && svn export ${PROJECT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/LaundredSheep-${PROJECT_VERSION}
  253. && echo "Compressing..."
  254. && cd ${CMAKE_BINARY_DIR}
  255. && tar -cjf ${CMAKE_BINARY_DIR}/LaundredSheep-${PROJECT_VERSION}.tar.bz2 ./LaundredSheep-${PROJECT_VERSION}
  256. && echo "Done, cleaning up"
  257. && rm -rf ${CMAKE_BINARY_DIR}/LaundredSheep-${PROJECT_VERSION}
  258. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  259. )
  260. endif()
  261. # ==== Checking if data folder exists ====
  262. if(NOT IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data)
  263. message( FATAL_ERROR "${CMAKE_CURRENT_SOURCE_DIR}/data folder doesn't exist" )
  264. endif()
  265. # ==== Checking if ls-assets folder exists ====
  266. if(CHECK_ASSETS)
  267. if((IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data/textures))
  268. message(STATUS "Assets found in data directory")
  269. elseif(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../ls-assets)
  270. set(LS_ASSETS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../ls-assets/)
  271. message(STATUS "Assets found")
  272. elseif(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../laundredsheep-assets)
  273. set(LS_ASSETS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../laundredsheep-assets/)
  274. message(STATUS "Assets found")
  275. else()
  276. set (CUR_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  277. get_filename_component(PARENT_DIR ${CUR_DIR} PATH)
  278. message( FATAL_ERROR "${PARENT_DIR}/ls-assets folder doesn't exist. "
  279. "Please download the ls-assets, or disable this test with -DCHECK_ASSETS=off." )
  280. endif()
  281. endif()
  282. # ==== Install target ====
  283. install(TARGETS laundredsheep RUNTIME DESTINATION ${LS_INSTALL_BINARY_DIR} BUNDLE DESTINATION .)
  284. install(DIRECTORY ${LS_DATA_DIR} DESTINATION ${LS_INSTALL_DATA_DIR} PATTERN ".svn" EXCLUDE PATTERN ".git" EXCLUDE)
  285. if(LS_ASSETS_DIR AND CHECK_ASSETS)
  286. install(DIRECTORY ${LS_ASSETS_DIR} DESTINATION ${LS_INSTALL_DATA_DIR}/data PATTERN ".svn" EXCLUDE PATTERN ".git" EXCLUDE)
  287. endif()
  288. #install(FILES ${LS_DATA_DIR}/laundredsheep.desktop DESTINATION share/applications)
  289. #install(FILES data/laundredsheep_32.png DESTINATION share/icons/hicolor/32x32/apps RENAME laundredsheep.png)
  290. #install(FILES data/laundredsheep_128.png DESTINATION share/icons/hicolor/128x128/apps RENAME laundredsheep.png)
  291. #install(FILES data/laundredsheep_32.png data/laundredsheep_128.png DESTINATION share/pixmaps)
  292. #install(FILES data/laundredsheep.appdata.xml DESTINATION share/appdata)
  293. if(MINGW)
  294. install(DIRECTORY ${CMAKE_BINARY_DIR}/bin/ DESTINATION ${LS_INSTALL_BINARY_DIR}
  295. FILES_MATCHING PATTERN "*.dll")
  296. endif()