Skip to content

Commit 9c32b8a

Browse files
committedDec 21, 2020
CMakeLists.txt
wrapped dependecies inside cmake targets removed some global setters (compiler flags) linking new cmake targets as private (this is good practice in general) FindFFmpeg.cmake wrapped components inside cmake targets
1 parent d92e44d commit 9c32b8a

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed
 

‎CMakeLists.txt

+48-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.0)
22

33
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
4+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
45

56
# Project
67
project(Stronghold)
@@ -13,41 +14,52 @@ endif()
1314
include_directories(src)
1415

1516
# Thirdparty
16-
include_directories(${CMAKE_SOURCE_DIR}/thirdparty/blast/)
17-
include_directories(${CMAKE_SOURCE_DIR}/thirdparty/duktape/)
18-
include_directories(${CMAKE_SOURCE_DIR}/thirdparty/cxxopts/include/)
19-
include_directories(${CMAKE_SOURCE_DIR}/thirdparty/filesystem/include/)
17+
18+
# blast
19+
add_library(blast thirdparty/blast/blast.c)
20+
set_target_properties(blast PROPERTIES
21+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/thirdparty/blast)
22+
23+
# cxxopts
24+
set(CXXOPTS_BUILD_TESTS OFF)
25+
set(CXXOPTS_BUILD_EXAMPLES OFF)
26+
27+
add_subdirectory(thirdparty/cxxopts)
28+
29+
# ghc filesystem
30+
add_subdirectory(thirdparty/filesystem)
2031

2132
# pthread
22-
find_package (Threads)
33+
find_package(Threads REQUIRED)
2334

2435
# SDL2
2536
find_package(SDL2 REQUIRED)
26-
include_directories(${SDL2_INCLUDE_DIR})
2737

2838
# OpenAL
2939
find_package(OpenAL REQUIRED)
30-
include_directories(${OPENAL_INCLUDE_DIR})
40+
add_library(OpenAL::OpenAL UNKNOWN IMPORTED)
41+
set_target_properties(OpenAL::OpenAL PROPERTIES
42+
IMPORTED_LOCATION ${OPENAL_LIBRARY}
43+
INTERFACE_INCLUDE_DIRECTORIES ${OPENAL_INCLUDE_DIR})
3144

3245
# FFmpeg
33-
find_package(FFmpeg REQUIRED)
34-
include_directories(${FFMPEG_INCLUDE_DIRS})
35-
include_directories(${SWSCALE_INCLUDE_DIRS})
46+
find_package(FFmpeg REQUIRED COMPONENTS
47+
AVCODEC AVFORMAT AVUTIL SWSCALE)
48+
3649

3750
# swresample
3851
find_package(Libswresample REQUIRED)
39-
include_directories( ${LIBSWRESAMPLE_INCLUDE_DIRS})
52+
add_library(SWRESAMPLE::SWRESAMPLE UNKNOWN IMPORTED)
53+
set_target_properties(SWRESAMPLE::SWRESAMPLE PROPERTIES
54+
IMPORTED_LOCATION ${LIBSWRESAMPLE_LIBRARIES}
55+
INTERFACE_INCLUDE_DIRECTORIES ${LIBSWRESAMPLE_INCLUDE_DIRS})
4056

4157
# Include sources / headers
4258
file(
4359
GLOB_RECURSE _source_list
4460
LIST_DIRECTORIES false
4561
"${CMAKE_SOURCE_DIR}/src/*.cpp*"
4662
"${CMAKE_SOURCE_DIR}/src/*.h*"
47-
"${CMAKE_SOURCE_DIR}/thirdparty/blast/*.c"
48-
"${CMAKE_SOURCE_DIR}/thirdparty/blast/*.h"
49-
"${CMAKE_SOURCE_DIR}/thirdparty/cxxopts/*.c"
50-
"${CMAKE_SOURCE_DIR}/thirdparty/cxxopts/*.h"
5163
)
5264

5365
foreach(_source IN ITEMS ${_source_list})
@@ -57,31 +69,41 @@ foreach(_source IN ITEMS ${_source_list})
5769
source_group("${_group_path}" FILES "${_source}")
5870
endforeach()
5971

72+
add_executable(Stronghold ${_source_list})
73+
74+
6075
if(MSVC)
61-
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
76+
target_compile_options(Stronghold PRIVATE -D_CRT_SECURE_NO_WARNINGS)
6277
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
6378
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
6479
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
6580
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
6681
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR} )
6782
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
6883
else()
69-
add_definitions(-Wno-reorder -pedantic-errors -Ofast -fno-fast-math)
84+
target_compile_options(Stronghold PRIVATE
85+
-Wno-reorder
86+
-pedantic-errors
87+
-Ofast
88+
-fno-fast-math)
7089
endif()
7190

72-
add_executable(Stronghold ${_source_list})
7391
set_target_properties(Stronghold PROPERTIES
7492
CXX_STANDARD 11
7593
CXX_STANDARD_REQUIRED YES
7694
CXX_EXTENSIONS NO
7795
)
7896

79-
target_link_libraries(
80-
Stronghold
81-
${SDL2_LIBRARY}
82-
${OPENAL_LIBRARY}
83-
${FFMPEG_LIBRARIES}
84-
${SWSCALE_LIBRARIES}
85-
${LIBSWRESAMPLE_LIBRARIES}
86-
${CMAKE_THREAD_LIBS_INIT}
97+
target_link_libraries(Stronghold
98+
PRIVATE Threads::Threads
99+
PRIVATE SDL2::SDL2
100+
PRIVATE OpenAL::OpenAL
101+
PRIVATE FFMPEG::AVCODEC
102+
PRIVATE FFMPEG::AVFORMAT
103+
PRIVATE FFMPEG::AVUTIL
104+
PRIVATE FFMPEG::SWSCALE
105+
PRIVATE SWRESAMPLE::SWRESAMPLE
106+
PRIVATE blast
107+
PRIVATE cxxopts
108+
PRIVATE ghc_filesystem
87109
)

‎cmake/FindFFmpeg.cmake

+9-1
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,13 @@ foreach (_component ${FFmpeg_FIND_COMPONENTS})
147147
list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
148148
endforeach ()
149149

150+
# Add imported targets for each component
151+
foreach (_component ${FFmpeg_FIND_COMPONENTS})
152+
add_library(FFMPEG::${_component} UNKNOWN IMPORTED)
153+
set_target_properties(FFMPEG::${_component} PROPERTIES
154+
IMPORTED_LOCATION ${${_component}_LIBRARIES}
155+
INTERFACE_INCLUDE_DIRECTORIES ${${_component}_INCLUDE_DIRS})
156+
endforeach()
157+
150158
# Give a nice error message if some of the required vars are missing.
151-
find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})
159+
find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})

0 commit comments

Comments
 (0)
Please sign in to comment.