hedgewars/CMakeLists.txt
branchwebgl
changeset 8330 aaefa587e277
parent 8096 453917e94e55
parent 8322 50369fa5053b
child 8338 cfad3738132c
equal deleted inserted replaced
8116:d24257910f8d 8330:aaefa587e277
     4 find_package(SDL_ttf)
     4 find_package(SDL_ttf)
     5 find_package(SDL_mixer)
     5 find_package(SDL_mixer)
     6 
     6 
     7 include(${CMAKE_SOURCE_DIR}/cmake_modules/FindSDL_Extras.cmake)
     7 include(${CMAKE_SOURCE_DIR}/cmake_modules/FindSDL_Extras.cmake)
     8 
     8 
     9 configure_file(${CMAKE_SOURCE_DIR}/hedgewars/config.inc.in ${CMAKE_CURRENT_BINARY_DIR}/config.inc)
     9 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.inc.in ${CMAKE_CURRENT_BINARY_DIR}/config.inc)
    10 
    10 
    11 #SOURCE AND PROGRAMS SECTION
    11 #SOURCE AND PROGRAMS SECTION
    12 set(hwengine_project ${CMAKE_SOURCE_DIR}/hedgewars/hwengine.pas)
    12 if(${LIBENGINE})
    13 set(engine_output_name "hwengine")
    13     set(engine_output_name "${CMAKE_SHARED_LIBRARY_PREFIX}hwengine${CMAKE_SHARED_LIBRARY_SUFFIX}")
       
    14     set(hwengine_project ${CMAKE_CURRENT_SOURCE_DIR}/hwLibrary.pas)
       
    15 else()
       
    16     set(engine_output_name "hwengine${CMAKE_EXECUTABLE_SUFFIX}")
       
    17     set(hwengine_project ${CMAKE_CURRENT_SOURCE_DIR}/hwengine.pas)
       
    18 endif()
       
    19 
       
    20 if (APPLE)
       
    21     set(required_fpc_version 2.6)
       
    22 else()
       
    23     set(required_fpc_version 2.2)
       
    24 endif()
    14 
    25 
    15 set(engine_sources
    26 set(engine_sources
    16     ${hwengine_project}
    27     ${hwengine_project}
    17     LuaPas.pas
    28     LuaPas.pas
    18     PNGh.pas
    29     PNGh.pas
    52     uLandTemplates.pas
    63     uLandTemplates.pas
    53     uLandTexture.pas
    64     uLandTexture.pas
    54     uLocale.pas
    65     uLocale.pas
    55     uMatrix.pas
    66     uMatrix.pas
    56     uMisc.pas
    67     uMisc.pas
    57     uMobile.pas
       
    58     uPhysFSLayer.pas
    68     uPhysFSLayer.pas
    59     uRandom.pas
    69     uRandom.pas
    60     uRender.pas
    70     uRender.pas
    61     uRenderUtils.pas
    71     uRenderUtils.pas
    62     uScript.pas
    72     uScript.pas
    79     options.inc
    89     options.inc
    80     adler32.pas
    90     adler32.pas
    81     ${CMAKE_CURRENT_BINARY_DIR}/config.inc
    91     ${CMAKE_CURRENT_BINARY_DIR}/config.inc
    82     )
    92     )
    83 
    93 
    84 if(LIBENGINE)
    94 if(${LIBENGINE})
    85     message(STATUS "Engine will be built as library (experimental)")
    95     message(WARNING "Engine will be built as library (experimental)")
    86     set(hwengine_project ${CMAKE_SOURCE_DIR}/hedgewars/hwLibrary.pas)
       
    87     set(pascal_flags "-dHWLIBRARY" ${pascal_flags})
    96     set(pascal_flags "-dHWLIBRARY" ${pascal_flags})
    88 
    97 
    89     # create position independent code, only required for x68_64 builds, similar to -fPIC
    98     # create position independent code, only required for x68_64 builds, similar to -fPIC
    90     if(CMAKE_SIZEOF_VOID_P MATCHES "8")
    99     if(CMAKE_SIZEOF_VOID_P MATCHES "8")
    91         set(pascal_flags "-Cg" ${pascal_flags})
   100         set(pascal_flags "-Cg" ${pascal_flags})
    92     endif(CMAKE_SIZEOF_VOID_P MATCHES "8")
   101     endif(CMAKE_SIZEOF_VOID_P MATCHES "8")
    93 
   102 
    94     # due to compiler/linker issues on Max OS X 10.6 -k-no_order_inits is needed to avoid linking fail
   103     # due to compiler/linker issues on Max OS X 10.6 -k-no_order_inits is needed to avoid linking fail
    95     if(APPLE AND current_macosx_version GREATER "10.5")
   104     if(APPLE AND current_macosx_version VERSION_GREATER "10.5")
    96         set(pascal_flags "-k-no_order_inits" ${pascal_flags})
   105         set(pascal_flags "-k-no_order_inits" ${pascal_flags})
    97     endif()
   106     endif()
    98 
   107     set(destination_dir ${target_library_install_dir})
    99     if(APPLE)
   108 else(${LIBENGINE})
   100         set(engine_output_name "libhwengine.dylib")
   109     set(destination_dir ${target_binary_install_dir})
   101     endif (APPLE)
   110 endif(${LIBENGINE})
   102 endif(LIBENGINE)
   111 
   103 
   112 
   104 
   113 # Check Freepascal version
   105 #PASCAL DETECTION SECTION
   114 find_package(Freepascal)
   106 IF(FPC)
   115 
   107     set(fpc_executable ${FPC})
   116 if (FPC_VERSION VERSION_LESS required_fpc_version)
   108 ELSE()
   117     message(FATAL_ERROR "Freepascal is too old, minimum version required is ${required_fpc_version}")
   109     find_program(fpc_executable fpc)
   118 endif()
   110 ENDIF()
   119 
   111 
       
   112 message(STATUS "Check for working FPC compiler: ${fpc_executable}")
       
   113 execute_process(COMMAND ${fpc_executable} -iV OUTPUT_VARIABLE fpc_output ERROR_VARIABLE fpc_error)
       
   114 if(fpc_error)
       
   115     message(STATUS "Check for working FPC compiler: ${fpc_executable} -- broken")
       
   116 else(fpc_error)
       
   117     message(STATUS "Check for working FPC compiler: ${fpc_executable} -- works")
       
   118 endif(fpc_error)
       
   119 
       
   120 string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" fpc_version "${fpc_output}")
       
   121 if(fpc_version)
       
   122     string(REGEX REPLACE "([0-9]+)\\.[0-9]+\\.[0-9]+" "\\1" fpc_vers_major "${fpc_version}")
       
   123     string(REGEX REPLACE "[0-9]+\\.([0-9]+)\\.[0-9]+" "\\1" fpc_vers_minor "${fpc_version}")
       
   124     string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" fpc_vers_patch "${fpc_version}")
       
   125     message(STATUS "Found Freepascal: ${fpc_executable} (version ${fpc_vers_major}.${fpc_vers_minor})")
       
   126     math(EXPR fpc_version "${fpc_vers_major}*10000 + ${fpc_vers_minor}*100 + ${fpc_vers_patch}")
       
   127 
       
   128     if(fpc_version LESS "020200")
       
   129         message(FATAL_ERROR "Minimum required version of FreePascal is 2.2.0")
       
   130     elseif(APPLE AND (fpc_version LESS "020600"))
       
   131         message(FATAL_ERROR "Minimum required version of FreePascal is 2.6.0 on Mac OS X")
       
   132     endif()
       
   133 else()
       
   134     message(FATAL_ERROR "No FreePascal compiler found!")
       
   135 endif()
       
   136 
       
   137 message(STATUS "Checking whether linker supports noexecstack flag")
       
   138 set(noexecstack_flags "-k-z" "-knoexecstack")
       
   139 file(WRITE ${EXECUTABLE_OUTPUT_PATH}/checkstack.pas "begin end.")
       
   140 
       
   141 execute_process(COMMAND ${fpc_executable} ${noexecstack_flags} checkstack.pas
       
   142     WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
       
   143     RESULT_VARIABLE testnoexecstack
       
   144     OUTPUT_QUIET ERROR_QUIET
       
   145     )
       
   146 
       
   147 if(${testnoexecstack})
       
   148     set (noexecstack_flags "")
       
   149     message(STATUS "Checking whether linker supports noexecstack flag -- no")
       
   150 else(${testnoexecstack})
       
   151     message(STATUS "Checking whether linker supports noexecstack flag -- yes")
       
   152 endif(${testnoexecstack})
       
   153 
   120 
   154 #DEPENDECIES AND EXECUTABLES SECTION
   121 #DEPENDECIES AND EXECUTABLES SECTION
   155 if(APPLE)
   122 if(APPLE)
   156     string(REGEX MATCH "[pP][pP][cC]+" powerpc_build "${CMAKE_OSX_ARCHITECTURES}")
   123     string(REGEX MATCH "[pP][pP][cC]+" powerpc_build "${CMAKE_OSX_ARCHITECTURES}")
   157     string(REGEX MATCH "[iI]386+" i386_build "${CMAKE_OSX_ARCHITECTURES}")
   124     string(REGEX MATCH "[iI]386+" i386_build "${CMAKE_OSX_ARCHITECTURES}")
   160     if(powerpc_build)
   127     if(powerpc_build)
   161         set(powerpc_build "powerpc")
   128         set(powerpc_build "powerpc")
   162     endif()
   129     endif()
   163 
   130 
   164     #on OSX we need to provide the SDL_main() function when building as executable
   131     #on OSX we need to provide the SDL_main() function when building as executable
   165     if(NOT LIBENGINE)
   132     if(NOT ${LIBENGINE})
   166         #let's look for the installed sdlmain file; if it is not found, let's build our own
   133         #let's look for the installed sdlmain file; if it is not found, let's build our own
   167         find_package(SDL REQUIRED)
   134         find_package(SDL REQUIRED)
   168         #remove the ";-framework Cocoa" from the SDL_LIBRARY variable
   135         #remove the ";-framework Cocoa" from the SDL_LIBRARY variable
   169         string(REGEX REPLACE "(.*);-.*" "\\1" sdl_dir "${SDL_LIBRARY}")
   136         string(REGEX REPLACE "(.*);-.*" "\\1" sdl_library_only "${SDL_LIBRARY}")
   170         #find libsdmain.a
   137         #find libsdmain.a
   171         find_file(SDLMAIN_LIB libSDLMain.a PATHS ${sdl_dir}/Resources/)
   138         find_file(SDLMAIN_LIB libSDLMain.a PATHS ${sdl_library_only}/Resources/)
   172 
   139 
   173         if(SDLMAIN_LIB MATCHES "SDLMAIN_LIB-NOTFOUND")
   140         if(SDLMAIN_LIB MATCHES "SDLMAIN_LIB-NOTFOUND")
   174             include_directories(${SDL_INCLUDE_DIR})
   141             include_directories(${SDL_INCLUDE_DIR})
   175             add_library (SDLmain STATIC sdlmain_osx/SDLMain.m)
   142             add_library (SDLmain STATIC sdlmain_osx/SDLMain.m)
   176             #add a dependency to the hwengine target
   143             #add a dependency to the hwengine target
   178             set(SDLMAIN_LIB "${LIBRARY_OUTPUT_PATH}/libSDLmain.a")
   145             set(SDLMAIN_LIB "${LIBRARY_OUTPUT_PATH}/libSDLmain.a")
   179         endif()
   146         endif()
   180 
   147 
   181         set(pascal_flags "-k${SDLMAIN_LIB}" ${pascal_flags})
   148         set(pascal_flags "-k${SDLMAIN_LIB}" ${pascal_flags})
   182     endif()
   149     endif()
   183     set(pascal_flags "-k${LIBRARY_OUTPUT_PATH}/libphysfs.a" ${pascal_flags})
       
   184 endif(APPLE)
   150 endif(APPLE)
   185 
   151 
   186 if(NOT NOPNG)
   152 if(NOT NOPNG)
   187     find_package(PNG)
   153     find_package(PNG)
   188     if(${PNG_FOUND})
   154     if(${PNG_FOUND})
   197     message(STATUS "Screenshots will be in BMP format per user request")
   163     message(STATUS "Screenshots will be in BMP format per user request")
   198 endif()
   164 endif()
   199 
   165 
   200 
   166 
   201 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
   167 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
   202 if(fpc_version LESS "020600")
   168 if(FPC_VERSION VERSION_LESS "2.6")
   203     #under some configurations CMAKE_BUILD_TOOL fails to pass on the jobserver, breaking parallel compilation
   169     #under some configurations CMAKE_BUILD_TOOL fails to pass on the jobserver, breaking parallel compilation
   204     #TODO: check if this is needed on windows too
       
   205     if(UNIX)
   170     if(UNIX)
   206         set(SAFE_BUILD_TOOL $(MAKE))
   171         set(SAFE_BUILD_TOOL $(MAKE))
   207     else()
   172     else()
   208         set(SAFE_BUILD_TOOL ${CMAKE_BUILD_TOOL})
   173         set(SAFE_BUILD_TOOL ${CMAKE_BUILD_TOOL})
   209     endif()
   174     endif()
   224         set(pascal_flags "-dUSE_VIDEO_RECORDING" ${pascal_flags})
   189         set(pascal_flags "-dUSE_VIDEO_RECORDING" ${pascal_flags})
   225         IF (WIN32)
   190         IF (WIN32)
   226             # there are some problems with linking our avwrapper as static lib, so link it as shared
   191             # there are some problems with linking our avwrapper as static lib, so link it as shared
   227             add_library(avwrapper SHARED videorec/avwrapper.c)
   192             add_library(avwrapper SHARED videorec/avwrapper.c)
   228             target_link_libraries(avwrapper ${FFMPEG_LIBRARIES})
   193             target_link_libraries(avwrapper ${FFMPEG_LIBRARIES})
   229             install(PROGRAMS "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_SHARED_LIBRARY_PREFIX}avwrapper${CMAKE_SHARED_LIBRARY_SUFFIX}" DESTINATION ${target_dir})
   194             install(PROGRAMS "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_SHARED_LIBRARY_PREFIX}avwrapper${CMAKE_SHARED_LIBRARY_SUFFIX}" DESTINATION ${target_library_install_dir})
   230         ELSE()
   195         ELSE()
   231             add_library(avwrapper STATIC videorec/avwrapper.c)
   196             add_library(avwrapper STATIC videorec/avwrapper.c)
   232             set(pascal_flags "-k${FFMPEG_LIBAVCODEC}" "-k${FFMPEG_LIBAVFORMAT}" "-k${FFMPEG_LIBAVUTIL}" ${pascal_flags})
   197             set(pascal_flags "-k${FFMPEG_LIBAVCODEC}" "-k${FFMPEG_LIBAVFORMAT}" "-k${FFMPEG_LIBAVUTIL}" ${pascal_flags})
   233         ENDIF()
   198         ENDIF()
   234     else()
   199     else()
   236     endif()
   201     endif()
   237 else()
   202 else()
   238     message(STATUS "Video recording disabled by user")
   203     message(STATUS "Video recording disabled by user")
   239 endif()
   204 endif()
   240 
   205 
   241 set(pascal_flags "-Fl${LIBRARY_OUTPUT_PATH}" ${pascal_flags})
   206 
   242 
   207 set(fpc_flags ${NOEXECSTACK_FLAGS} ${pascal_flags} ${hwengine_project})
   243 set(fpc_flags ${noexecstack_flags} ${pascal_flags} ${hwengine_project})
   208 
   244 
   209 if(NOT APPLE)
   245 IF(NOT APPLE)
       
   246     #here is the command for standard executables or for shared library
   210     #here is the command for standard executables or for shared library
   247     add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}${CMAKE_EXECUTABLE_SUFFIX}"
   211     add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}"
   248         COMMAND "${fpc_executable}"
   212         COMMAND "${FPC_EXECUTABLE}"
   249         ARGS ${fpc_flags}
   213         ARGS ${fpc_flags} -o${engine_output_name}
   250         MAIN_DEPENDENCY ${hwengine_project}
   214         MAIN_DEPENDENCY ${hwengine_project}
   251         DEPENDS ${engine_sources}
   215         DEPENDS ${engine_sources}
   252         )
   216         )
   253 ELSE()
   217 else()
   254     #these are the dependencies for building a universal binary on Mac OS X
   218     #these are the dependencies for building a universal binary on Mac OS X
   255     foreach (build_arch ${powerpc_build} ${i386_build} ${x86_64_build})
   219     foreach (build_arch ${powerpc_build} ${i386_build} ${x86_64_build})
   256         set(lipo_args_list "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}" ${lipo_args_list})
   220         set(lipo_args_list "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}" ${lipo_args_list})
   257         add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}"
   221         add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}"
   258             COMMAND "${fpc_executable}"
   222             COMMAND "${FPC_EXECUTABLE}"
   259             ARGS ${fpc_flags} -ohwengine.${build_arch} -P${build_arch}
   223             ARGS ${fpc_flags} -ohwengine.${build_arch} -P${build_arch}
   260             MAIN_DEPENDENCY ${hwengine_project}
   224             MAIN_DEPENDENCY ${hwengine_project}
   261             DEPENDS ${engine_sources}
   225             DEPENDS ${engine_sources}
   262             )
   226             )
   263         add_custom_target(hwengine.${build_arch} ALL DEPENDS "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}")
   227         add_custom_target(hwengine.${build_arch} ALL DEPENDS "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}")
       
   228         add_custom_command(TARGET hwengine.${build_arch} POST_BUILD
       
   229             COMMAND "install_name_tool"
       
   230             ARGS -id @executable_path/../Frameworks/${engine_output_name}
       
   231                 ${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}
       
   232             )
   264     endforeach()
   233     endforeach()
   265 
   234 
   266     add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}"
   235     add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}"
   267         COMMAND "lipo"
   236         COMMAND "lipo"
   268         ARGS ${lipo_args_list} -create -output ${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}
   237         ARGS ${lipo_args_list} -create -output ${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}
   269         DEPENDS ${lipo_args_list}
   238         DEPENDS ${lipo_args_list}
   270         )
   239         )
   271 ENDIF()
   240 endif()
   272 
   241 
   273 
   242 
   274 add_custom_target(${engine_output_name} ALL DEPENDS "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}${CMAKE_EXECUTABLE_SUFFIX}")
   243 add_custom_target(${engine_output_name} ALL DEPENDS "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}")
   275 
   244 
   276 #when system Lua is not found we need to compile it before engine
   245 #when system Lua is not found we need to compile it before engine
   277 if(NOT LUA_FOUND)
   246 if(NOT LUA_FOUND)
   278     add_dependencies(${engine_output_name} lua)
   247     add_dependencies(${engine_output_name} lua)
   279 endif()
   248 endif()
   286 if(${FFMPEG_FOUND})
   255 if(${FFMPEG_FOUND})
   287     add_dependencies(${engine_output_name} avwrapper)
   256     add_dependencies(${engine_output_name} avwrapper)
   288 endif()
   257 endif()
   289 
   258 
   290 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
   259 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
   291 if((fpc_version LESS "020600") AND (NOVIDEOREC OR NOT ${FFMPEG_FOUND}))
   260 if((FPC_VERSION VERSION_LESS "2.6") AND (NOVIDEOREC OR NOT ${FFMPEG_FOUND}))
   292     add_dependencies(${engine_output_name} ENGINECLEAN)
   261     add_dependencies(${engine_output_name} ENGINECLEAN)
   293 endif()
   262 endif()
   294 
   263 
   295 install(PROGRAMS "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}${CMAKE_EXECUTABLE_SUFFIX}" DESTINATION ${target_dir})
   264 install(PROGRAMS "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}" DESTINATION ${destination_dir})