Start using CMake targets instead of variables

Moved most of the variables to targets in the main src.

Made libraries in repo export targets.

Refactor STATIC flags a little and add more.

This commit definitely breaks a lot of things like tests
but the main game builds at least with MSVC.
fix-squashed-planets
MrSimbax 2020-07-10 18:49:54 +02:00
parent f2d91cde80
commit df65862c49
8 changed files with 146 additions and 226 deletions

View File

@ -144,9 +144,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# Include cmake directory with some additional scripts
set(CMAKE_MODULE_PATH "${colobot_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
# MSVC needs different flags if linking statically
option(MSVC_STATIC "Link statically when using MSVC" OFF)
# Compiler detection
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
@ -187,12 +184,14 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message(STATUS "Detected MSVC compiler")
set(NORMAL_CXX_FLAGS "/wd\"4244\" /wd\"4309\" /wd\"4800\" /wd\"4996\" /wd\"4351\" /EHsc") # disable some useless warnings
if(MSVC_STATIC)
if(BUILD_STATIC)
set(RELEASE_CXX_FLAGS "/MT /Ox")
set(DEBUG_CXX_FLAGS "/MTd /ZI")
else(MSVC_STATIC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
set(RELEASE_CXX_FLAGS "/MD /Ox")
set(DEBUG_CXX_FLAGS "/MDd /ZI")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL$<$<CONFIG:Debug>:Debug>")
endif()
set(TEST_CXX_FLAGS "")
add_definitions(-DNOEXCEPT= -DHAS_MSVC_EXCEPTION_BUG)
@ -245,9 +244,6 @@ option(TESTS "Build tests" OFF)
# Building tool programs can be enabled/disabled
option(TOOLS "Build tool programs" OFF)
# CBot can also be a static library
option(CBOT_STATIC "Build CBot as static libary" OFF)
# Generate desktop files, manpage, etc.
option(DESKTOP "Generate desktop files, manpages, etc" ON)
@ -257,11 +253,20 @@ option(INSTALL_DOCS "Install Doxygen-generated documentation" OFF)
# Build OpenAL sound support
option(OPENAL_SOUND "Build OpenAL sound support" ON)
# Build Colobot with static libraries
option(BUILD_STATIC "The default linking option for external libraries" OFF)
# CBot can also be a static library
option(CBOT_STATIC "Build CBot as static libary" ${BUILD_STATIC})
# This is useful in case you want to use static boost libraries
option(BOOST_STATIC "Link with static boost libraries" OFF)
option(BOOST_STATIC "Link with static boost libraries" ${BUILD_STATIC})
# This is useful on Windows, if linking against standard GLEW dll fails
option(GLEW_STATIC "Link statically with GLEW" OFF)
option(GLEW_STATIC "Link statically with GLEW" ${BUILD_STATIC})
# Link statically with LibSndFile
option(SNDFILE_STATIC "Link statically with LibSndFile" ${BUILD_STATIC})
# Sometimes helpful if there is a different version of gtest installed on system vs bundled
option(FORCE_BUNDLED_GTEST "Force the use of bundled gtest" OFF)
@ -269,7 +274,6 @@ option(FORCE_BUNDLED_GTEST "Force the use of bundled gtest" OFF)
# This is for use with colobot-lint tool
option(COLOBOT_LINT_BUILD "Generate some additional CMake targets for use with colobot-lint" OFF)
# Default build type if not given is debug
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not specified - assuming debug")
@ -301,12 +305,12 @@ set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ADDITIONALVERSION "1.51" "1.51.0")
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
set(GLEW_USE_STATIC_LIBS ${GLEW_STATIC})
find_package(GLEW REQUIRED)
if (OPENAL_SOUND)
find_package(OpenAL REQUIRED)
include_directories(${OPENAL_INCLUDE_DIR})
find_package(LibSndFile REQUIRED)
find_package(SndFile REQUIRED)
endif()
@ -345,10 +349,9 @@ include("${colobot_SOURCE_DIR}/cmake/colobot-lint.cmake")
# MSVC specific settings
##
set(WINGETOPT 0)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
if(MSVC)
message(STATUS "Adding MSVC-specific options")
set(CBOT_STATIC 1) # only this works for some reason
set(WINGETOPT 1) # use wingetopt library
# Hide console in release builds
@ -360,7 +363,6 @@ endif()
##
# Localename
##
set(LOCALENAME_INCLUDE_DIR ${colobot_SOURCE_DIR}/lib/localename)
add_subdirectory(lib/localename)
@ -368,7 +370,6 @@ add_subdirectory(lib/localename)
# Wingetopt
##
if(WINGETOPT)
set(WINGETOPT_INCLUDE_DIR ${colobot_SOURCE_DIR}/lib/wingetopt/src)
add_subdirectory(lib/wingetopt)
endif()
@ -456,7 +457,7 @@ if(TESTS)
add_subdirectory(${GTEST_SRC_DIR} lib/gtest)
# Hippomocks library
set(HIPPOMOCKS_INCLUDE_DIR ${colobot_SOURCE_DIR}/lib/hippomocks)
add_subdirectory(${colobot_SOURCE_DIR}/lib/hippomocks)
# Tests targets
enable_testing()

View File

@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 3.17)
include_directories(. include)
add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
# gtest-all.cc includes all other sources
add_library(gtest STATIC src/gtest-all.cc)
target_include_directories(gtest PUBLIC . include)
target_compile_definitions(gtest PUBLIC GTEST_HAS_TR1_TUPLE=0)

View File

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.17)
add_library(hippomocks INTERFACE hippomocks.h)
target_include_directories(hippomocks INTERFACE ${CMAKE_CURRENT_LIST_DIR})

View File

@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.17)
add_library(localename STATIC localename.c)
add_library(localename STATIC localename.c)
target_include_directories(localename PUBLIC ${CMAKE_CURRENT_LIST_DIR})

View File

@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 3.17)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
add_library(wingetopt STATIC src/getopt.c src/getopt.h)
target_include_directories(wingetopt PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src)

View File

@ -1,4 +1,14 @@
set(SOURCES
if(CBOT_STATIC)
add_library(CBot STATIC)
else()
add_library(CBot SHARED)
install(TARGETS CBot
LIBRARY DESTINATION ${COLOBOT_INSTALL_LIB_DIR}
ARCHIVE DESTINATION ${COLOBOT_INSTALL_LIB_DIR}
RUNTIME DESTINATION ${COLOBOT_INSTALL_BIN_DIR})
endif()
target_sources(CBot PRIVATE
CBot.h
CBotCStack.cpp
CBotCStack.h
@ -152,26 +162,5 @@ set(SOURCES
stdlib/stdlib.h
stdlib/stdlib_public.h
)
# Includes
set(LOCAL_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/..
)
set(SYSTEM_INCLUDES
${Boost_INCLUDE_DIRS}
)
include_directories(${LOCAL_INCLUDES})
include_directories(SYSTEM ${SYSTEM_INCLUDES})
if(CBOT_STATIC)
add_library(CBot STATIC ${SOURCES})
else()
add_library(CBot SHARED ${SOURCES})
install(TARGETS CBot LIBRARY
DESTINATION ${COLOBOT_INSTALL_LIB_DIR}
ARCHIVE DESTINATION ${COLOBOT_INSTALL_LIB_DIR}
RUNTIME DESTINATION ${COLOBOT_INSTALL_BIN_DIR})
endif()
target_include_directories(CBot PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
target_link_libraries(CBot PRIVATE Boost::headers)

View File

@ -1,10 +1,10 @@
# Compile flags as defined in global CMakeLists
# Todo: replace with target properties
set(CMAKE_CXX_FLAGS "${COLOBOT_CXX_FLAGS} ${MXE_CFLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE ${COLOBOT_CXX_FLAGS_RELEASE})
set(CMAKE_CXX_FLAGS_DEBUG ${COLOBOT_CXX_FLAGS_DEBUG})
# Subdirectories
add_subdirectory(CBot)
if(TOOLS)
@ -13,122 +13,12 @@ endif()
add_subdirectory(graphics/opengl/shaders)
# Optional libraries
set(OPTIONAL_LIBS "")
set(OPTIONAL_INCLUDES "")
if(OPENAL_SOUND)
set(OPTIONAL_LIBS ${OPENAL_LIBRARY})
set(OPTIONAL_INCLUDES ${OPENAL_INCLUDE_DIR})
endif()
if(WINGETOPT)
set(OPTIONAL_LIBS ${OPTIONAL_LIBS} wingetopt)
set(OPTIONAL_INCLUDES ${OPTIONAL_INCLUDES} ${WINGETOPT_INCLUDE_DIR})
endif()
# Additional libraries per platform
if(MXE) # MXE requires special treatment
set(PLATFORM_LIBS ${MXE_LIBS})
elseif(PLATFORM_WINDOWS)
# because it isn't included in standard linking libraries
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
find_library(LIBINTL_LIBRARY NAMES intl.lib libintl)
if(${MSVC_STATIC})
if (${OPENAL_SOUND})
find_library(FLAC_LIBRARY NAMES flac.lib)
find_library(VORBIS_LIBRARY NAMES vorbis.lib)
find_library(VORBISENC_LIBRARY NAMES vorbisenc.lib)
find_library(OGG_LIBRARY NAMES ogg.lib)
set(OPENAL_MSVC_LIBS
${FLAC_LIBRARY}
${VORBIS_LIBRARY}
${VORBISENC_LIBRARY}
${OGG_LIBRARY}
)
endif()
find_library(BZ2_LIBRARY NAMES bz2.lib)
find_library(JPEG_LIBRARY NAMES jpeg.lib)
find_library(TIFF_LIBRARY NAMES tiff.lib)
find_library(LZMA_LIBRARY NAMES lzma.lib)
find_library(FREETYPE_LIBRARY NAMES freetype.lib)
find_library(ICONV_LIBRARY NAMES libiconv.lib)
find_library(CHARSET_LIBRARY NAMES libcharset.lib)
set(MSVC_LIBS
${LIBINTL_LIBRARY}
${OPENAL_MSVC_LIBS}
${JPEG_LIBRARY}
${TIFF_LIBRARY}
${BZ2_LIBRARY}
${LZMA_LIBRARY}
${FREETYPE_LIBRARY}
${ICONV_LIBRARY}
${CHARSET_LIBRARY}
winmm.lib
dxguid.lib
imm32.lib
ole32.lib
oleaut32.lib
version.lib
wsock32.lib
ws2_32.lib
setupapi.lib
)
else(${MSVC_STATIC})
set(MSVC_LIBS ${LIBINTL_LIBRARY})
endif()
set(PLATFORM_LIBS ${MSVC_LIBS})
else()
set(PLATFORM_LIBS "-lintl")
endif()
elseif(PLATFORM_GNU)
set(PLATFORM_LIBS "")
elseif(PLATFORM_LINUX)
# for clock_gettime
set(PLATFORM_LIBS "-lrt")
elseif(PLATFORM_MACOSX)
find_library(LIBINTL_LIBRARY NAMES intl libintl)
find_path(LIBINTL_INCLUDE_PATH NAMES libintl.h)
set(PLATFORM_LIBS ${LIBINTL_LIBRARY})
elseif(PLATFORM_FREEBSD)
find_library(LIBINTL_LIBRARY NAMES intl libintl)
find_path(LIBINTL_INCLUDE_PATH NAMES libintl.h)
set(PLATFORM_LIBS ${LIBINTL_LIBRARY})
endif()
# Configure file
configure_file(common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h)
configure_file(common/version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/version.h)
set(OPENAL_SRC "")
if(OPENAL_SOUND)
set(OPENAL_SRC
sound/oalsound/alsound.cpp
sound/oalsound/buffer.cpp
sound/oalsound/channel.cpp
sound/oalsound/check.cpp
sound/oalsound/alsound.h
sound/oalsound/buffer.h
sound/oalsound/channel.h
sound/oalsound/check.h
)
endif()
set(RES_FILES "")
if(PLATFORM_WINDOWS)
set(RES_FILES "../desktop/colobot.rc")
endif()
# Source files
set(BASE_SOURCES
${OPENAL_SRC}
# Todo: split this monstrosity into smaller libraries (graphics, math, ui, etc.)
add_library(colobotbase STATIC
app/app.cpp
app/app.h
app/controller.cpp
@ -579,77 +469,121 @@ set(BASE_SOURCES
ui/studio.h
)
set(MAIN_SOURCES
app/main.cpp
${RES_FILES}
target_include_directories(colobotbase PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}
)
# Libraries
set(LIBS
# Main libraries
target_link_libraries(colobotbase PUBLIC
CBot
localename
${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARY}
${SDL2_TTF_LIBRARY}
${OPENGL_LIBRARY}
${PNG_LIBRARIES}
${GLEW_LIBRARY}
${Boost_LIBRARIES}
${LIBSNDFILE_LIBRARY}
${OPTIONAL_LIBS}
${PLATFORM_LIBS}
${PHYSFS_LIBRARY}
SDL2::Core
SDL2::Image
SDL2::TTF
OpenGL::GL
PNG::PNG
GLEW::GLEW
Boost::headers
Boost::filesystem
Boost::regex
PhysFS::PhysFS
SndFile::sndfile
)
# Optional libraries
if(OPENAL_SOUND)
target_sources(colobotbase PRIVATE
sound/oalsound/alsound.cpp
sound/oalsound/buffer.cpp
sound/oalsound/channel.cpp
sound/oalsound/check.cpp
sound/oalsound/alsound.h
sound/oalsound/buffer.h
sound/oalsound/channel.h
sound/oalsound/check.h
)
target_link_libraries(colobotbase PUBLIC OpenAL::OpenAL)
endif()
if(WINGETOPT)
target_link_libraries(colobotbase PUBLIC wingetopt)
endif()
# Additional libraries per platform
if(MXE) # MXE requires special treatment
target_link_libraries(colobotbase PUBLIC ${MXE_LIBS})
elseif(PLATFORM_WINDOWS)
# because it isn't included in standard linking libraries
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
if(BUILD_STATIC)
find_package(Intl REQUIRED)
#todo figure out why those dependencies are needed and find them in a better way
find_library(BZ2_LIBRARY NAMES bz2.lib)
find_library(JPEG_LIBRARY NAMES jpeg.lib)
find_library(TIFF_LIBRARY NAMES tiff.lib)
find_library(LZMA_LIBRARY NAMES lzma.lib)
find_library(FREETYPE_LIBRARY NAMES freetype.lib)
find_library(ICONV_LIBRARY NAMES libiconv.lib)
find_library(CHARSET_LIBRARY NAMES libcharset.lib)
target_link_libraries(colobotbase PUBLIC
Gettext::Intl
${JPEG_LIBRARY}
${TIFF_LIBRARY}
${BZ2_LIBRARY}
${LZMA_LIBRARY}
${FREETYPE_LIBRARY}
${ICONV_LIBRARY}
${CHARSET_LIBRARY}
# why
winmm.lib
dxguid.lib
imm32.lib
ole32.lib
oleaut32.lib
version.lib
wsock32.lib
ws2_32.lib
setupapi.lib
)
endif()
else()
target_link_libraries(colobotbase PUBLIC -lintl)
endif()
elseif(PLATFORM_GNU)
# nothing
elseif(PLATFORM_LINUX)
# for clock_gettime
target_link_libraries(colobotbase PUBLIC -lrt)
elseif(PLATFORM_MACOSX)
find_package(Intl REQUIRED)
target_link_libraries(colobotbase PUBLIC Gettext::Intl)
elseif(PLATFORM_FREEBSD)
find_package(Intl REQUIRED)
target_link_libraries(colobotbase PUBLIC Gettext::Intl)
endif()
set(COLOBOT_LIBS ${LIBS} PARENT_SCOPE)
# Includes
set(LOCAL_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}
)
set(SYSTEM_INCLUDES
${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
${GLEW_INCLUDE_PATH}
${Boost_INCLUDE_DIRS}
${LIBSNDFILE_INCLUDE_DIR}
${LOCALENAME_INCLUDE_DIR}
${PHYSFS_INCLUDE_PATH}
${LIBINTL_INCLUDE_PATH}
${OPTIONAL_INCLUDES}
)
set(COLOBOT_LOCAL_INCLUDES ${LOCAL_INCLUDES} PARENT_SCOPE)
set(COLOBOT_SYSTEM_INCLUDES ${SYSTEM_INCLUDES} PARENT_SCOPE)
include_directories(${LOCAL_INCLUDES})
include_directories(SYSTEM ${SYSTEM_INCLUDES})
# Link directories
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/CBot
)
# Targets
add_executable(colobot app/main.cpp)
if(PLATFORM_WINDOWS)
target_sources(colobot PRIVATE ../desktop/colobot.rc)
endif()
target_link_libraries(colobot colobotbase SDL2::Main)
add_library(colobotbase STATIC ${BASE_SOURCES})
add_executable(colobot ${MAIN_SOURCES})
target_link_libraries(colobot colobotbase ${LIBS})
# Install
install(TARGETS colobot RUNTIME DESTINATION ${COLOBOT_INSTALL_BIN_DIR})
if(NOT CBOT_STATIC)
set_target_properties(colobot PROPERTIES INSTALL_RPATH ${COLOBOT_INSTALL_LIB_DIR})
endif()
# Linter-specific
if(COLOBOT_LINT_BUILD)
add_fake_header_sources("src")
endif()

View File

@ -1,15 +1,9 @@
set(CONVERT_MODEL_SOURCES
add_executable(convert_model
../common/logger.cpp
../graphics/model/model.cpp
../graphics/model/model_mesh.cpp
../graphics/model/model_input.cpp
../graphics/model/model_output.cpp
convert_model.cpp
)
include_directories(. ..)
include_directories(SYSTEM ${SDL_INCLUDE_DIR})
add_executable(convert_model ${CONVERT_MODEL_SOURCES})
convert_model.cpp)
target_include_directories(convert_model PRIVATE . ..) #todo SDL2