Skip to content

Commit

Permalink
build json11 as external dep and link it using the target syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed Apr 20, 2019
1 parent 634bbd1 commit f940b52
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Expand Up @@ -7,9 +7,6 @@
[submodule "dronecore-proto"]
path = src/backend/proto
url = https://github.com/dronecore/DroneCore-Proto.git
[submodule "json11"]
path = src/plugins/mission/third_party/json11
url = https://github.com/dronecore/json11
[submodule "third_party/cpp_rsc"]
path = src/third_party/cpp_rsc
url = https://github.com/orex/cpp_rsc.git
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -6,7 +6,7 @@ include_directories(

configure_file(version.h.in version.h)

add_library(dronecode_sdk ${LIBRARY_TYPE}
add_library(dronecode_sdk
call_every_handler.cpp
connection.cpp
curl_wrapper.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/action/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_action ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_action
action.cpp
action_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/calibration/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_calibration ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_calibration
calibration.cpp
calibration_impl.cpp
calibration_statustext_parser.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/camera/CMakeLists.txt
Expand Up @@ -10,7 +10,7 @@ if(NOT MSVC)
include_directories(${RSC_H_DIR})
endif()

add_library(dronecode_sdk_camera ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_camera
camera.cpp
camera_impl.cpp
camera_definition.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/follow_me/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_follow_me ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_follow_me
follow_me.cpp
follow_me_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/gimbal/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_gimbal ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_gimbal
gimbal.cpp
gimbal_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/info/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_info ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_info
info.cpp
info_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/log_files/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_log_files ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_log_files
log_files.cpp
log_files_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/logging/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_logging ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_logging
logging.cpp
logging_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/mavlink_passthrough/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_mavlink_passthrough ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_mavlink_passthrough
mavlink_passthrough.cpp
mavlink_passthrough_impl.cpp
)
Expand Down
15 changes: 4 additions & 11 deletions src/plugins/mission/CMakeLists.txt
@@ -1,4 +1,6 @@
add_library(dronecode_sdk_mission ${PLUGIN_LIBRARY_TYPE}
find_package(json11 REQUIRED)

add_library(dronecode_sdk_mission
mission.cpp
mission_impl.cpp
mission_item.cpp
Expand All @@ -7,18 +9,15 @@ add_library(dronecode_sdk_mission ${PLUGIN_LIBRARY_TYPE}

include_directories(
${PROJECT_SOURCE_DIR}/core
SYSTEM third_party/json11
)

set_target_properties(dronecode_sdk_mission
PROPERTIES COMPILE_FLAGS ${warnings}
)

add_subdirectory(third_party/json11)

target_link_libraries(dronecode_sdk_mission
dronecode_sdk
json11
json11::json11
)

install(FILES
Expand All @@ -38,12 +37,6 @@ PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

if(ANDROID)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/json11/libjson11.a
DESTINATION ${lib_path}
)
endif()

list(APPEND UNIT_TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/mission_import_qgc_test.cpp
)
Expand Down
1 change: 0 additions & 1 deletion src/plugins/mission/third_party/json11
Submodule json11 deleted from 59c962
2 changes: 1 addition & 1 deletion src/plugins/mission_raw/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_mission_raw ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_mission_raw
mission_raw.cpp
mission_raw_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/offboard/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_offboard ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_offboard
offboard.cpp
offboard_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/param/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_param ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_param
param.cpp
param_impl.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/telemetry/CMakeLists.txt
@@ -1,4 +1,4 @@
add_library(dronecode_sdk_telemetry ${PLUGIN_LIBRARY_TYPE}
add_library(dronecode_sdk_telemetry
telemetry.cpp
telemetry_impl.cpp
math_conversions.cpp
Expand Down
33 changes: 9 additions & 24 deletions third_party/CMakeLists.txt
Expand Up @@ -2,34 +2,19 @@ cmake_minimum_required(VERSION 3.1)

project(third_party)

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/install" CACHE PATH "CMake install prefix path for dependencies" FORCE)
endif()

include(cmake/build_target.cmake)

set(THIRD_PARTY_SOURCE_DIR "${PROJECT_SOURCE_DIR}")
set(THIRD_PARTY_BINARY_DIR "${THIRD_PARTY_SOURCE_DIR}/build")
set(THIRD_PARTY_INSTALL_DIR "${THIRD_PARTY_BINARY_DIR}/install")

list(APPEND CMAKE_PREFIX_PATH "${THIRD_PARTY_INSTALL_DIR}")
list(APPEND CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/build/install")
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE)

build_target(${THIRD_PARTY_SOURCE_DIR}/tinyxml2 ${THIRD_PARTY_BINARY_DIR}/tinyxml2 ${THIRD_PARTY_INSTALL_DIR})
build_target(${THIRD_PARTY_SOURCE_DIR}/zlib ${THIRD_PARTY_BINARY_DIR}/zlib ${THIRD_PARTY_INSTALL_DIR})

# In SUPERBUILD, we want to link dependencies statically. Because zlib doesn't
# allow to disable building the shared libs, we remove them manually.
FILE(GLOB_RECURSE LIBZ_SHARED_LIBS "${THIRD_PARTY_INSTALL_DIR}/*libz.so*")
foreach(LIBZ_SHARED_LIB ${LIBZ_SHARED_LIBS})
FILE(REMOVE "${LIBZ_SHARED_LIB}")
endforeach()

build_target(${THIRD_PARTY_SOURCE_DIR}/curl ${THIRD_PARTY_BINARY_DIR}/curl ${THIRD_PARTY_INSTALL_DIR})
build_target(json11)
build_target(tinyxml2)
build_target(zlib)
build_target(curl)

if(${BUILD_BACKEND})
build_target(${THIRD_PARTY_SOURCE_DIR}/libressl ${THIRD_PARTY_BINARY_DIR}/libressl ${THIRD_PARTY_INSTALL_DIR})
build_target(${THIRD_PARTY_SOURCE_DIR}/cares ${THIRD_PARTY_BINARY_DIR}/cares ${THIRD_PARTY_INSTALL_DIR})
build_target(${THIRD_PARTY_SOURCE_DIR}/protobuf ${THIRD_PARTY_BINARY_DIR}/protobuf ${THIRD_PARTY_INSTALL_DIR})
build_target(${THIRD_PARTY_SOURCE_DIR}/grpc ${THIRD_PARTY_BINARY_DIR}/grpc ${THIRD_PARTY_INSTALL_DIR})
build_target(libressl)
build_target(cares)
build_target(protobuf)
build_target(grpc)
endif()
6 changes: 5 additions & 1 deletion third_party/cmake/build_target.cmake
@@ -1,6 +1,10 @@
include(ProcessorCount)

function(build_target TARGET_SOURCE_DIR TARGET_BINARY_DIR TARGET_INSTALL_DIR)
function(build_target TARGET_NAME)
set(TARGET_SOURCE_DIR "${PROJECT_SOURCE_DIR}/${TARGET_NAME}")
set(TARGET_BINARY_DIR "${PROJECT_SOURCE_DIR}/build/${TARGET_NAME}")
set(TARGET_INSTALL_DIR "${PROJECT_SOURCE_DIR}/build/install")

file(MAKE_DIRECTORY ${TARGET_BINARY_DIR})

if(DEFINED ${CMAKE_GENERATOR_PLATFORM})
Expand Down
36 changes: 36 additions & 0 deletions third_party/json11/CMakeLists.txt
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.1)

project(external-json11)
include(ExternalProject)

list(APPEND CMAKE_ARGS
"-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}"
"-DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE}"
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
"-DBUILD_SHARED_LIBS=OFF"
)

if(ANDROID)
list(APPEND CMAKE_ARGS
"-DANDROID_ABI:STRING=${ANDROID_ABI}"
"-DANDROID_STL:STRING=${ANDROID_STL}"
"-DANDROID_PLATFORM=${ANDROID_PLATFORM}"
)
elseif(IOS)
list(APPEND CMAKE_ARGS
"-DIOS_PLATFORM=${IOS_PLATFORM}"
)
endif()

message(STATUS "Preparing external project \"json11\" with args:")
foreach(CMAKE_ARG ${CMAKE_ARGS})
message(STATUS "-- ${CMAKE_ARG}")
endforeach()

ExternalProject_add(
json11
URL https://github.com/dropbox/json11/archive/v1.0.0.tar.gz
PREFIX json11
PATCH_COMMAND patch -p1 < ${PROJECT_SOURCE_DIR}/export_config.patch
CONFIGURE_COMMAND ${CMAKE_COMMAND} "${CMAKE_ARGS}" <SOURCE_DIR>
)
40 changes: 40 additions & 0 deletions third_party/json11/export_config.patch
@@ -0,0 +1,40 @@
commit 1ff431620c61c3e68a8c9e68dd2652ef66da9f95
Author: fegorsch <flix.gorschi@gmail.com>
Date: Tue May 8 15:23:14 2018 +0200

Export json11Config.cmake on install

So you can easily use installed builds from other CMake projects by
setting json11_DIR.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 819c36f..899f06c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,7 +23,10 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
endif()

add_library(json11 json11.cpp)
-target_include_directories(json11 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+target_include_directories(json11 PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+ $<INSTALL_INTERFACE:.>
+)
target_compile_options(json11
PRIVATE -fPIC -fno-rtti -fno-exceptions -Wall)

@@ -52,6 +55,11 @@ if (JSON11_BUILD_TESTS)
target_link_libraries(json11_test json11)
endif()

-install(TARGETS json11 DESTINATION lib/${CMAKE_LIBRARY_ARCHITECTURE})
+install(TARGETS json11 EXPORT json11Config DESTINATION lib
+ ARCHIVE DESTINATION lib
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin
+ INCLUDES DESTINATION include)
-install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/json11.hpp" DESTINATION include/${CMAKE_LIBRARY_ARCHITECTURE})
-install(FILES "${CMAKE_CURRENT_BINARY_DIR}/json11.pc" DESTINATION lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig)
+install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/json11.hpp" DESTINATION include)
+install(FILES "${CMAKE_CURRENT_BINARY_DIR}/json11.pc" DESTINATION lib/pkgconfig)
+install(EXPORT json11Config NAMESPACE json11:: DESTINATION cmake)
7 changes: 1 addition & 6 deletions third_party/tinyxml2/CMakeLists.txt
Expand Up @@ -10,11 +10,6 @@ list(APPEND CMAKE_ARGS
"-DBUILD_SHARED_LIBS=OFF"
)

if(DEFINED ${CMAKE_GENERATOR_PLATFORM})
set(PLATFORM_ARGUMENT "-A${CMAKE_GENERATOR_PLATFORM}")
message(FATAL_ERROR "blaaaaaaaaaah ${PLATFORM_ARGUMENT}")
endif()

if(ANDROID)
list(APPEND CMAKE_ARGS
"-DANDROID_ABI:STRING=${ANDROID_ABI}"
Expand All @@ -28,7 +23,7 @@ elseif(IOS)
endif()

message(STATUS "Preparing external project \"tinyxml2\" with args:")
foreach(CMAKE_ARG ${PLATFORM_ARGUMENT} ${CMAKE_ARGS})
foreach(CMAKE_ARG ${CMAKE_ARGS})
message(STATUS "-- ${CMAKE_ARG}")
endforeach()

Expand Down
1 change: 1 addition & 0 deletions third_party/zlib/CMakeLists.txt
Expand Up @@ -30,5 +30,6 @@ ExternalProject_add(
zlib
URL https://github.com/madler/zlib/archive/v1.2.11.tar.gz
PREFIX zlib
PATCH_COMMAND patch -p1 < ${PROJECT_SOURCE_DIR}/build_shared_libs.patch
CONFIGURE_COMMAND ${CMAKE_COMMAND} "${CMAKE_ARGS}" <SOURCE_DIR>
)
89 changes: 89 additions & 0 deletions third_party/zlib/build_shared_libs.patch
@@ -0,0 +1,89 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0fe939d..1873459 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -123,7 +123,7 @@ set(ZLIB_SRCS
zutil.c
)

-if(NOT MINGW)
+if(BUILD_SHARED_LIBS AND NOT MINGW)
set(ZLIB_DLL_SRCS
win32/zlib1.rc # If present will override custom build rule below.
)
@@ -167,7 +167,7 @@ file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
"\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})

-if(MINGW)
+if(BUILD_SHARED_LIBS AND MINGW)
# This gets us DLL resource information when compiling on MinGW.
if(NOT CMAKE_RC_COMPILER)
set(CMAKE_RC_COMPILER windres.exe)
@@ -181,37 +181,42 @@ if(MINGW)
-o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
-i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
-endif(MINGW)
-
-add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
-add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
-set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
-set_target_properties(zlib PROPERTIES SOVERSION 1)
-
-if(NOT CYGWIN)
- # This property causes shared libraries on Linux to have the full version
- # encoded into their final filename. We disable this on Cygwin because
- # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
- # seems to be the default.
- #
- # This has no effect with MSVC, on that platform the version info for
- # the DLL comes from the resource file win32/zlib1.rc
- set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
endif()

+add_library(zlib ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
+
if(UNIX)
# On unix-like platforms the library is almost always called libz
- set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
- if(NOT APPLE)
- set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
- endif()
-elseif(BUILD_SHARED_LIBS AND WIN32)
- # Creates zlib1.dll when building shared library version
- set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
+ set_target_properties(zlib PROPERTIES OUTPUT_NAME z)
+endif()
+
+if(BUILD_SHARED_LIBS)
+ set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
+ set_target_properties(zlib PROPERTIES SOVERSION 1)
+
+ if(NOT CYGWIN)
+ # This property causes shared libraries on Linux to have the full version
+ # encoded into their final filename. We disable this on Cygwin because
+ # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
+ # seems to be the default.
+ #
+ # This has no effect with MSVC, on that platform the version info for
+ # the DLL comes from the resource file win32/zlib1.rc
+ set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
+ endif()
+
+ if(UNIX AND NOT APPLE)
+ set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
+ endif()
+
+ if(WIN32)
+ # Creates zlib1.dll when building shared library version
+ set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
+ endif()
endif()

if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
- install(TARGETS zlib zlibstatic
+ install(TARGETS zlib
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )

0 comments on commit f940b52

Please sign in to comment.