1From 6cb16322decd643fed9de332d9cda77f7738b7af Mon Sep 17 00:00:00 2001
2From: Adrian Perez de Castro <aperez@igalia.com>
3Date: Mon, 7 Sep 2020 12:14:22 +0300
4Subject: [PATCH] CMake: Allow using BUILD_SHARED_LIBS to choose static/shared
5 libs
6
7By convention projects using CMake which can build either static or
8shared libraries use a BUILD_SHARED_LIBS flag to allow selecting between
9both: the add_library() command automatically switches between both using
10this variable when the library kind is not passed to add_library(). It
11is also usual to expose the BUILD_SHARED_LIBS as an user-facing setting
12with the option() command.
13
14This way, the following will both work as expected:
15
16   % cmake -DBUILD_SHARED_LIBS=OFF ...
17   % cmake -DBUILS_SHARED_LIBS=ON ...
18
19This is helpful for distributions which need (or want) to build only
20static libraries.
21
22Signed-off-by: Adrian Perez de Castro <aperez@igalia.com>
23[Upstream status: https://github.com/google/brotli/pull/655]
24---
25 CMakeLists.txt        | 46 ++++++++++++++-----------------------------
26 c/fuzz/test_fuzzer.sh |  6 +++---
27 2 files changed, 18 insertions(+), 34 deletions(-)
28
29diff --git a/CMakeLists.txt b/CMakeLists.txt
30index 4ff3401..f889311 100644
31--- a/CMakeLists.txt
32+++ b/CMakeLists.txt
33@@ -6,6 +6,8 @@ cmake_minimum_required(VERSION 2.8.6)
34
35 project(brotli C)
36
37+option(BUILD_SHARED_LIBS "Build shared libraries" ON)
38+
39 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
40   message(STATUS "Setting build type to Release as none was specified.")
41   set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
42@@ -137,10 +139,6 @@ set(BROTLI_LIBRARIES_CORE brotlienc brotlidec brotlicommon)
43 set(BROTLI_LIBRARIES ${BROTLI_LIBRARIES_CORE} ${LIBM_LIBRARY})
44 mark_as_advanced(BROTLI_LIBRARIES)
45
46-set(BROTLI_LIBRARIES_CORE_STATIC brotlienc-static brotlidec-static brotlicommon-static)
47-set(BROTLI_LIBRARIES_STATIC ${BROTLI_LIBRARIES_CORE_STATIC} ${LIBM_LIBRARY})
48-mark_as_advanced(BROTLI_LIBRARIES_STATIC)
49-
50 if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
51   add_definitions(-DOS_LINUX)
52 elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
53@@ -161,29 +159,25 @@ transform_sources_list("scripts/sources.lst" "${CMAKE_CURRENT_BINARY_DIR}/source
54 include("${CMAKE_CURRENT_BINARY_DIR}/sources.lst.cmake")
55
56 if(BROTLI_EMSCRIPTEN)
57-  set(BROTLI_SHARED_LIBS "")
58-else()
59-  set(BROTLI_SHARED_LIBS brotlicommon brotlidec brotlienc)
60-  add_library(brotlicommon SHARED ${BROTLI_COMMON_C})
61-  add_library(brotlidec SHARED ${BROTLI_DEC_C})
62-  add_library(brotlienc SHARED ${BROTLI_ENC_C})
63+  set(BUILD_SHARED_LIBS OFF)
64 endif()
65
66-set(BROTLI_STATIC_LIBS brotlicommon-static brotlidec-static brotlienc-static)
67-add_library(brotlicommon-static STATIC ${BROTLI_COMMON_C})
68-add_library(brotlidec-static STATIC ${BROTLI_DEC_C})
69-add_library(brotlienc-static STATIC ${BROTLI_ENC_C})
70+add_library(brotlicommon ${BROTLI_COMMON_C})
71+add_library(brotlidec ${BROTLI_DEC_C})
72+add_library(brotlienc ${BROTLI_ENC_C})
73
74 # Older CMake versions does not understand INCLUDE_DIRECTORIES property.
75 include_directories(${BROTLI_INCLUDE_DIRS})
76
77-foreach(lib IN LISTS BROTLI_SHARED_LIBS)
78-  target_compile_definitions(${lib} PUBLIC "BROTLI_SHARED_COMPILATION" )
79-  string(TOUPPER "${lib}" LIB)
80-  set_target_properties (${lib} PROPERTIES DEFINE_SYMBOL "${LIB}_SHARED_COMPILATION")
81-endforeach()
82+if(BUILD_SHARED_LIBS)
83+  foreach(lib brotlicommon brotlidec brotlienc)
84+    target_compile_definitions(${lib} PUBLIC "BROTLI_SHARED_COMPILATION" )
85+    string(TOUPPER "${lib}" LIB)
86+    set_target_properties (${lib} PROPERTIES DEFINE_SYMBOL "${LIB}_SHARED_COMPILATION")
87+  endforeach()
88+endif()
89
90-foreach(lib IN LISTS BROTLI_SHARED_LIBS BROTLI_STATIC_LIBS)
91+foreach(lib brotlicommon brotlidec brotlienc)
92   target_link_libraries(${lib} ${LIBM_LIBRARY})
93   set_property(TARGET ${lib} APPEND PROPERTY INCLUDE_DIRECTORIES ${BROTLI_INCLUDE_DIRS})
94   set_target_properties(${lib} PROPERTIES
95@@ -200,9 +194,6 @@ target_link_libraries(brotlidec brotlicommon)
96 target_link_libraries(brotlienc brotlicommon)
97 endif()
98
99-target_link_libraries(brotlidec-static brotlicommon-static)
100-target_link_libraries(brotlienc-static brotlicommon-static)
101-
102 # For projects stuck on older versions of CMake, this will set the
103 # BROTLI_INCLUDE_DIRS and BROTLI_LIBRARIES variables so they still
104 # have a relatively easy way to use Brotli:
105@@ -216,7 +207,7 @@ endif()
106
107 # Build the brotli executable
108 add_executable(brotli ${BROTLI_CLI_C})
109-target_link_libraries(brotli ${BROTLI_LIBRARIES_STATIC})
110+target_link_libraries(brotli ${BROTLI_LIBRARIES})
111
112 # Installation
113 if(NOT BROTLI_EMSCRIPTEN)
114@@ -233,13 +224,6 @@ if(NOT BROTLI_BUNDLED_MODE)
115     RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
116   )
117
118-  install(
119-    TARGETS ${BROTLI_LIBRARIES_CORE_STATIC}
120-    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
121-    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
122-    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
123-  )
124-
125   install(
126     DIRECTORY ${BROTLI_INCLUDE_DIRS}/brotli
127     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
128--
1292.28.0
130
131