Skip to content

Commit 6607a48

Browse files
committed
CMake: refactor inclusion of external libraries.
This commit makes common external packages always be included through find_package to eliminate differences in variables set, wraps find_package for vendored libraries on Windows to factor out common code, and removes miscellaneous useless code elsewhere in dependency handling. This also fixes a problem where pkg-config would pick up `build` libraries instead of `host` when cross-compiling.
1 parent 7265121 commit 6607a48

File tree

8 files changed

+142
-121
lines changed

8 files changed

+142
-121
lines changed

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
[submodule "extlib/libpng"]
66
path = extlib/libpng
77
url = https://github.com/glennrp/libpng
8-
[submodule "extlib/libfreetype"]
9-
path = extlib/libfreetype
8+
[submodule "extlib/freetype"]
9+
path = extlib/freetype
1010
url = http://git.sv.nongnu.org/r/freetype/freetype2.git
1111
[submodule "extlib/libdxfrw"]
1212
path = extlib/libdxfrw

CMakeLists.txt

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -121,56 +121,26 @@ message(STATUS "Using in-tree libdxfrw")
121121
add_subdirectory(extlib/libdxfrw)
122122

123123
if(WIN32)
124-
# Configure Freetype first. If done later, it will notice that
125-
# zlib is available, try to use it and promptly break on MSVC
126-
# in a very obscure way. Given that the only use of zlib, bzip2
127-
# and png support is in support for extremely obsolete Unix fonts,
128-
# we don't care.
129-
find_package(Freetype)
130-
131-
if(NOT FREETYPE_FOUND)
132-
message(STATUS "Using in-tree libfreetype")
133-
134-
add_subdirectory(extlib/libfreetype EXCLUDE_FROM_ALL)
135-
136-
set(FREETYPE_LIBRARY
137-
freetype)
138-
set(FREETYPE_INCLUDE_DIRS
139-
"${CMAKE_SOURCE_DIR}/extlib/libfreetype/include")
140-
find_package(Freetype REQUIRED)
141-
endif()
142-
143-
find_package(ZLIB)
144-
145-
if(NOT ZLIB_FOUND)
146-
message(STATUS "Using in-tree zlib")
147-
add_subdirectory(extlib/zlib EXCLUDE_FROM_ALL)
148-
149-
message(STATUS "Using in-tree libpng")
150-
set(ZLIB_LIBRARY
151-
zlibstatic)
152-
set(ZLIB_INCLUDE_DIR
153-
"${CMAKE_SOURCE_DIR}/extlib/zlib"
154-
"${CMAKE_BINARY_DIR}/extlib/zlib")
155-
find_package(ZLIB REQUIRED)
156-
endif()
157-
158-
find_package(PNG)
159-
160-
if(NOT PNG_FOUND)
161-
message(STATUS "Using in-tree libpng")
162-
163-
set(SKIP_INSTALL_ALL
164-
ON)
165-
add_subdirectory(extlib/libpng EXCLUDE_FROM_ALL)
166-
167-
set(PNG_LIBRARY
168-
png16_static)
169-
set(PNG_PNG_INCLUDE_DIR
170-
"${CMAKE_SOURCE_DIR}/extlib/libpng"
171-
"${CMAKE_BINARY_DIR}/extlib/libpng")
172-
find_package(PNG REQUIRED)
173-
endif()
124+
include(FindVendoredPackage)
125+
126+
find_vendored_package(Freetype freetype
127+
WITH_ZLIB OFF
128+
WITH_BZip2 OFF
129+
WITH_PNG OFF
130+
WITH_HarfBuzz OFF
131+
FREETYPE_LIBRARY freetype
132+
FREETYPE_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extlib/freetype/include)
133+
134+
find_vendored_package(ZLIB zlib
135+
ZLIB_LIBRARY zlibstatic
136+
ZLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/zlib)
137+
list(APPEND ZLIB_INCLUDE_DIR ${CMAKE_BINARY_DIR}/extlib/zlib)
138+
139+
find_vendored_package(PNG libpng
140+
SKIP_INSTALL_ALL ON
141+
PNG_LIBRARY png16_static
142+
PNG_PNG_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/libpng)
143+
list(APPEND PNG_PNG_INCLUDE_DIR ${CMAKE_BINARY_DIR}/extlib/libpng)
174144

175145
if(NOT MINGW)
176146
message(STATUS "Using prebuilt SpaceWare")
@@ -183,20 +153,25 @@ if(WIN32)
183153
elseif(APPLE)
184154
set(CMAKE_FIND_FRAMEWORK LAST)
185155

156+
find_package(ZLIB REQUIRED)
186157
find_package(PNG REQUIRED)
187158
find_package(Freetype REQUIRED)
159+
188160
find_library(APPKIT_LIBRARY AppKit REQUIRED)
189161
else() # Linux and compatible systems
190162
find_package(Backtrace)
191163
find_package(SpaceWare)
192164

193-
# Use freedesktop's pkg-config to locate everything.
165+
find_package(ZLIB REQUIRED)
166+
find_package(PNG REQUIRED)
167+
find_package(Freetype REQUIRED)
168+
169+
# Use freedesktop's pkg-config to locate everything else.
194170
find_package(PkgConfig REQUIRED)
195-
pkg_check_modules(ZLIB REQUIRED zlib)
196-
pkg_check_modules(PNG REQUIRED libpng)
197171
pkg_check_modules(FONTCONFIG REQUIRED fontconfig)
198172
pkg_check_modules(JSONC REQUIRED json-c)
199173
pkg_check_modules(FREETYPE REQUIRED freetype2)
174+
pkg_check_modules(CAIRO REQUIRED cairo)
200175

201176
set(HAVE_GTK TRUE)
202177
if(GUI STREQUAL "gtk3")

cmake/DisableWarnings.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Disables all warnings on MSVC and GNU-compatible compilers.
2+
3+
function(disable_warnings)
4+
if(CMAKE_C_COMPILER_ID STREQUAL GNU OR CMAKE_C_COMPILER_ID STREQUAL Clang)
5+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w" PARENT_SCOPE)
6+
elseif(CMAKE_C_COMPILER_ID STREQUAL MSVC)
7+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0" PARENT_SCOPE)
8+
endif()
9+
10+
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w" PARENT_SCOPE)
12+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL MSVC)
13+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W0" PARENT_SCOPE)
14+
endif()
15+
endfunction()

cmake/FindVendoredPackage.cmake

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Find the given library in the system locations, or build in-tree if not found.
2+
#
3+
# Arguments:
4+
# PKG_NAME - name of the package as passed to find_package
5+
# PKG_PATH - name of the source tree relative to extlib/
6+
#
7+
# The rest of the arguments are VARIABLE VALUE pairs. If the library is not found,
8+
# every VARIABLE will be set to VALUE and find_package will be rerun with the REQUIRED flag.
9+
# Regardless of where the library was found, only the specfied VARIABLEs that start with
10+
# ${PKG_NAME} will be set in the parent scope.
11+
#
12+
# All warnings in the in-tree package are disabled.
13+
14+
include(DisableWarnings)
15+
16+
function(find_vendored_package PKG_NAME PKG_PATH)
17+
find_package(${PKG_NAME})
18+
19+
set(cfg_name)
20+
foreach(item ${ARGN})
21+
if(NOT cfg_name)
22+
set(cfg_name ${item})
23+
else()
24+
set(${cfg_name} ${item})
25+
set(cfg_name)
26+
endif()
27+
endforeach()
28+
29+
disable_warnings()
30+
31+
string(TOUPPER ${PKG_NAME} VAR_NAME)
32+
if(NOT ${VAR_NAME}_FOUND)
33+
message(STATUS "Using in-tree ${PKG_PATH}")
34+
set(${VAR_NAME}_IN_TREE YES CACHE INTERNAL "")
35+
36+
add_subdirectory(extlib/${PKG_PATH} EXCLUDE_FROM_ALL)
37+
find_package(${PKG_NAME} REQUIRED)
38+
elseif(${VAR_NAME}_IN_TREE)
39+
add_subdirectory(extlib/${PKG_PATH} EXCLUDE_FROM_ALL)
40+
endif()
41+
42+
# Now put everything we just discovered into the cache.
43+
set(cfg_name)
44+
foreach(item ${ARGN} ${VAR_NAME}_FOUND)
45+
if(NOT cfg_name)
46+
set(cfg_name ${item})
47+
else()
48+
if(cfg_name MATCHES "^${VAR_NAME}")
49+
set(${cfg_name} "${${cfg_name}}" CACHE INTERNAL "")
50+
endif()
51+
set(cfg_name)
52+
endif()
53+
endforeach()
54+
endfunction()

cmake/Toolchain-mingw32.cmake

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
SET(CMAKE_SYSTEM_NAME Windows)
1+
set(CMAKE_SYSTEM_NAME Windows)
22

3-
SET(TRIPLE i686-w64-mingw32)
3+
set(TRIPLE i686-w64-mingw32)
44

5-
SET(CMAKE_C_COMPILER ${TRIPLE}-gcc)
6-
SET(CMAKE_CXX_COMPILER ${TRIPLE}-g++)
7-
SET(CMAKE_RC_COMPILER ${TRIPLE}-windres)
5+
set(CMAKE_C_COMPILER ${TRIPLE}-gcc)
6+
set(CMAKE_CXX_COMPILER ${TRIPLE}-g++)
7+
set(CMAKE_RC_COMPILER ${TRIPLE}-windres)
88

9-
SET(CMAKE_FIND_ROOT_PATH /usr/${TRIPLE})
9+
set(CMAKE_FIND_ROOT_PATH /usr/${TRIPLE})
1010

1111
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
1212
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
1313
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
14+
15+
set(ENV{PKG_CONFIG_LIBDIR} /usr/${TRIPLE}/lib/pkgconfig)

cmake/Toolchain-mingw64.cmake

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
SET(CMAKE_SYSTEM_NAME Windows)
1+
set(CMAKE_SYSTEM_NAME Windows)
22

3-
SET(TRIPLE x86_64-w64-mingw32)
3+
set(TRIPLE x86_64-w64-mingw32)
44

5-
SET(CMAKE_C_COMPILER ${TRIPLE}-gcc)
6-
SET(CMAKE_CXX_COMPILER ${TRIPLE}-g++)
7-
SET(CMAKE_RC_COMPILER ${TRIPLE}-windres)
5+
set(CMAKE_C_COMPILER ${TRIPLE}-gcc)
6+
set(CMAKE_CXX_COMPILER ${TRIPLE}-g++)
7+
set(CMAKE_RC_COMPILER ${TRIPLE}-windres)
88

9-
SET(CMAKE_FIND_ROOT_PATH /usr/${TRIPLE})
9+
set(CMAKE_FIND_ROOT_PATH /usr/${TRIPLE})
1010

1111
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
1212
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
1313
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
14+
15+
set(ENV{PKG_CONFIG_LIBDIR} /usr/${TRIPLE}/lib/pkgconfig)
Submodule freetype updated from 0000000 to 44accb9

src/CMakeLists.txt

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
# global
2-
31
include(GNUInstallDirs)
42

5-
include_directories(
6-
${OPENGL_INCLUDE_DIR}
7-
${PNG_INCLUDE_DIRS}
8-
${FREETYPE_INCLUDE_DIRS})
9-
10-
link_directories(
11-
${PNG_LIBRARY_DIRS}
12-
${FREETYPE_LIBRARY_DIRS})
13-
14-
add_definitions(
15-
${PNG_CFLAGS_OTHER})
3+
# configuration
164

175
include_directories(
186
${CMAKE_CURRENT_SOURCE_DIR}
19-
${CMAKE_CURRENT_SOURCE_DIR}/built
207
${CMAKE_CURRENT_BINARY_DIR})
218

22-
if(SPACEWARE_FOUND)
23-
include_directories(
24-
${SPACEWARE_INCLUDE_DIR})
25-
endif()
26-
279
set(HAVE_SPACEWARE ${SPACEWARE_FOUND})
28-
set(HAVE_GTK ${GTKMM_FOUND})
2910
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
3011
${CMAKE_CURRENT_BINARY_DIR}/config.h)
3112

@@ -75,16 +56,28 @@ if(NOT WIN32)
7556
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
7657
endif()
7758

78-
# platform dependencies
59+
# solvespace dependencies
60+
61+
include_directories(
62+
${OPENGL_INCLUDE_DIR}
63+
${ZLIB_INCLUDE_DIR}
64+
${PNG_PNG_INCLUDE_DIR}
65+
${FREETYPE_INCLUDE_DIRS})
66+
67+
if(SPACEWARE_FOUND)
68+
include_directories(
69+
${SPACEWARE_INCLUDE_DIR})
70+
endif()
7971

8072
if(WIN32)
8173
set(platform_SOURCES
8274
platform/w32main.cpp)
8375

8476
set(platform_LIBRARIES
85-
comctl32)
77+
comctl32
78+
${SPACEWARE_LIBRARIES})
8679
elseif(APPLE)
87-
add_definitions(
80+
add_compile_options(
8881
-fobjc-arc)
8982

9083
set(platform_SOURCES
@@ -98,29 +91,19 @@ elseif(APPLE)
9891
set(platform_LIBRARIES
9992
${APPKIT_LIBRARY})
10093
elseif(HAVE_GTK)
101-
include_directories(
102-
${GTKMM_INCLUDE_DIRS}
103-
${JSONC_INCLUDE_DIRS}
104-
${FONTCONFIG_INCLUDE_DIRS})
105-
106-
link_directories(
107-
${GTKMM_LIBRARY_DIRS}
108-
${JSONC_LIBRARY_DIRS}
109-
${FONTCONFIG_LIBRARY_DIRS})
110-
111-
add_definitions(
112-
${GTKMM_CFLAGS_OTHER}
113-
${JSONC_CFLAGS_OTHER}
114-
${FONTCONFIG_CFLAGS_OTHER})
115-
11694
set(platform_SOURCES
11795
platform/gtkmain.cpp
11896
render/rendergl.cpp)
11997

12098
set(platform_LIBRARIES
121-
${GTKMM_LIBRARIES}
122-
${JSONC_LIBRARIES}
123-
${FONTCONFIG_LIBRARIES})
99+
${Backtrace_LIBRARIES}
100+
${SPACEWARE_LIBRARIES})
101+
102+
foreach(pkg_config_lib GTKMM JSONC FONTCONFIG)
103+
include_directories(${${pkg_config_lib}_INCLUDE_DIRS})
104+
link_directories(${${pkg_config_lib}_LIBRARY_DIRS})
105+
list(APPEND platform_LIBRARIES ${${pkg_config_lib}_LIBRARIES})
106+
endforeach()
124107
endif()
125108

126109
# solvespace executable
@@ -199,26 +182,16 @@ add_dependencies(solvespace
199182
target_link_libraries(solvespace
200183
dxfrw
201184
${OPENGL_LIBRARIES}
202-
${PNG_LIBRARIES}
203-
${ZLIB_LIBRARIES}
204-
${FREETYPE_LIBRARIES}
185+
${ZLIB_LIBRARY}
186+
${PNG_LIBRARY}
187+
${FREETYPE_LIBRARY}
205188
${platform_LIBRARIES})
206189

207190
if(WIN32 AND NOT MINGW)
208191
set_target_properties(solvespace PROPERTIES
209192
LINK_FLAGS "/MANIFEST:NO /SAFESEH:NO")
210193
endif()
211194

212-
if(SPACEWARE_FOUND)
213-
target_link_libraries(solvespace
214-
${SPACEWARE_LIBRARIES})
215-
endif()
216-
217-
if(Backtrace_FOUND)
218-
target_link_libraries(solvespace
219-
${Backtrace_LIBRARIES})
220-
endif()
221-
222195
if(APPLE)
223196
foreach(lib ${platform_BUNDLED_LIBS})
224197
get_filename_component(name ${lib} NAME)

0 commit comments

Comments
 (0)