Skip to content

Commit

Permalink
Merge pull request #59 from xplpc/better-cmake-samples
Browse files Browse the repository at this point in the history
better cmake sample config
  • Loading branch information
paulocoutinhox committed Nov 22, 2023
2 parents 886f28c + 345be60 commit eb4ccff
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 142 deletions.
187 changes: 89 additions & 98 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ set(XPLPC_ADD_CUSTOM_DATA OFF CACHE BOOL "Build Custom Data")
set(XPLPC_CUSTOM_DATA_PATH "cxx/custom" CACHE STRING "Custom Data Path")
set(XPLPC_CUSTOM_DATA_INCLUDE_PATH "cxx/custom/include" CACHE STRING "Custom Data Include Path")
set(XPLPC_ENABLE_SERIALIZER_FOR_JSON ON CACHE BOOL "Enable Serializer For Json")
set(XPLPC_ENABLE_TESTS OFF CACHE BOOL "Enable Tests")
set(XPLPC_ENABLE_SAMPLES OFF CACHE BOOL "Enable Samples")
set(XPLPC_ENABLE_INTERFACE OFF CACHE BOOL "Enable Interface")
set(XPLPC_BUILD_TESTS OFF CACHE BOOL "Build Tests")
set(XPLPC_BUILD_SAMPLE OFF CACHE BOOL "Build Sample")
set(XPLPC_INC_FILES "" CACHE INTERNAL "Include Files")
set(XPLPC_SRC_FILES "" CACHE INTERNAL "Source Files")
set(XPLPC_TESTS_INC_FILES "" CACHE INTERNAL "Tests Include Files")
Expand Down Expand Up @@ -103,105 +103,87 @@ if(XPLPC_ADD_CUSTOM_DATA)
endif()

# target data
if (XPLPC_ENABLE_SAMPLES)
if(XPLPC_TARGET STREQUAL "cxx-static" OR XPLPC_TARGET STREQUAL "cxx-shared")
add_compile_definitions(XPLPC_TARGET_CXX)
add_subdirectory(cxx/sample)
if(XPLPC_TARGET STREQUAL "cxx-static")
add_compile_definitions(XPLPC_TARGET_CXX)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} STATIC "${XPLPC_ALL_FILES}")
elseif(XPLPC_TARGET STREQUAL "cxx-shared")
add_compile_definitions(XPLPC_TARGET_CXX)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
elseif(XPLPC_TARGET STREQUAL "c-static")
add_compile_definitions(XPLPC_TARGET_C)
add_subdirectory(c/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_executable(${XPLPC_PROJECT_NAME} "${XPLPC_ALL_FILES}")
elseif(XPLPC_TARGET STREQUAL "c-static" OR XPLPC_TARGET STREQUAL "c-shared")
add_compile_definitions(XPLPC_TARGET_C)
add_subdirectory(c/sample)
add_subdirectory(c/lib)
set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} STATIC "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC c/lib/include)
elseif(XPLPC_TARGET STREQUAL "c-shared")
add_compile_definitions(XPLPC_TARGET_C)
add_subdirectory(c/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_executable(${XPLPC_PROJECT_NAME} "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC c/lib/include)
set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC c/lib/include)
elseif(XPLPC_TARGET STREQUAL "kotlin")
add_compile_definitions(XPLPC_TARGET_KOTLIN)
add_subdirectory(jni/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC jni/lib/include)
elseif(XPLPC_TARGET STREQUAL "swift")
enable_language(Swift)
set(CMAKE_Swift_LANGUAGE_VERSION 5)

if (XPLPC_ENABLE_LOG)
set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} -D XPLPC_ENABLE_LOG")
endif()
else()
if(XPLPC_TARGET STREQUAL "cxx-static")
add_compile_definitions(XPLPC_TARGET_CXX)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} STATIC "${XPLPC_ALL_FILES}")
elseif(XPLPC_TARGET STREQUAL "cxx-shared")
add_compile_definitions(XPLPC_TARGET_CXX)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
elseif(XPLPC_TARGET STREQUAL "c-static")
add_compile_definitions(XPLPC_TARGET_C)
add_subdirectory(c/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} STATIC "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC c/lib/include)
elseif(XPLPC_TARGET STREQUAL "c-shared")
add_compile_definitions(XPLPC_TARGET_C)
add_subdirectory(c/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC c/lib/include)
elseif(XPLPC_TARGET STREQUAL "kotlin")
add_compile_definitions(XPLPC_TARGET_KOTLIN)
add_subdirectory(jni/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC jni/lib/include)
elseif(XPLPC_TARGET STREQUAL "swift")
enable_language(Swift)
set(CMAKE_Swift_LANGUAGE_VERSION 5)

if (XPLPC_ENABLE_LOG)
set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} -D XPLPC_ENABLE_LOG")
endif()

add_compile_definitions(XPLPC_TARGET_SWIFT)
add_subdirectory(objc/lib)
add_subdirectory(swift/lib)
add_subdirectory(swift/support)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC objc/lib/include)

set_target_properties(${XPLPC_PROJECT_NAME} PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER "com.xplpc.library"
VERSION ${XPLPC_VERSION} # "current version" in semantic format in Mach-O binary file
SOVERSION ${XPLPC_VERSION_CODE} # "compatibility version" in semantic format in Mach-O binary file
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH YES
XCODE_ATTRIBUTE_SKIP_INSTALL NO
XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++"
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.xplpc.library"
XCODE_ATTRIBUTE_DEFINES_MODULE YES
XCODE_ATTRIBUTE_MODULEMAP_FILE "swift/support/module.modulemap"
XCODE_ATTRIBUTE_BUILD_LIBRARY_FOR_DISTRIBUTION YES
PUBLIC_HEADER "${XPLPC_PUBLIC_HEADER_FILES}"
MACOSX_FRAMEWORK_BUNDLE_VERSION "${XPLPC_VERSION}" # machine readable
MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${XPLPC_VERSION}" # user visible
)
elseif(XPLPC_TARGET STREQUAL "wasm")
set(XPLPC_WASM_LINKER_FLAGS "--bind -sMALLOC=emmalloc -sWASM_BIGINT=1 -sALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -sMODULARIZE=1 -sEXPORT_ES6=1 -s \"EXPORTED_FUNCTIONS=['_malloc', '_free']\"")
set(XPLPC_WASM_COMPILER_FLAGS "-fwasm-exceptions")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${XPLPC_WASM_LINKER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XPLPC_WASM_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XPLPC_WASM_COMPILER_FLAGS}")

add_compile_definitions(XPLPC_TARGET_WASM)
add_subdirectory(wasm/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_executable(${XPLPC_PROJECT_NAME} "${XPLPC_ALL_FILES}")
else()
message(FATAL_ERROR "Target is invalid: ${XPLPC_TARGET}")
endif()
add_compile_definitions(XPLPC_TARGET_SWIFT)
add_subdirectory(objc/lib)
add_subdirectory(swift/lib)
add_subdirectory(swift/support)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_library(${XPLPC_PROJECT_NAME} SHARED "${XPLPC_ALL_FILES}")
target_include_directories(${XPLPC_PROJECT_NAME} PUBLIC objc/lib/include)

set_target_properties(${XPLPC_PROJECT_NAME} PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER "com.xplpc.library"
VERSION ${XPLPC_VERSION} # "current version" in semantic format in Mach-O binary file
SOVERSION ${XPLPC_VERSION_CODE} # "compatibility version" in semantic format in Mach-O binary file
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH YES
XCODE_ATTRIBUTE_SKIP_INSTALL NO
XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++"
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.xplpc.library"
XCODE_ATTRIBUTE_DEFINES_MODULE YES
XCODE_ATTRIBUTE_MODULEMAP_FILE "swift/support/module.modulemap"
XCODE_ATTRIBUTE_BUILD_LIBRARY_FOR_DISTRIBUTION YES
PUBLIC_HEADER "${XPLPC_PUBLIC_HEADER_FILES}"
MACOSX_FRAMEWORK_BUNDLE_VERSION "${XPLPC_VERSION}" # machine readable
MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${XPLPC_VERSION}" # user visible
)
elseif(XPLPC_TARGET STREQUAL "wasm")
set(XPLPC_WASM_LINKER_FLAGS "--bind -sMALLOC=emmalloc -sWASM_BIGINT=1 -sALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -sMODULARIZE=1 -sEXPORT_ES6=1 -s \"EXPORTED_FUNCTIONS=['_malloc', '_free']\"")
set(XPLPC_WASM_COMPILER_FLAGS "-fwasm-exceptions")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${XPLPC_WASM_LINKER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XPLPC_WASM_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XPLPC_WASM_COMPILER_FLAGS}")

add_compile_definitions(XPLPC_TARGET_WASM)
add_subdirectory(wasm/lib)

set(XPLPC_ALL_FILES ${XPLPC_INC_FILES} ${XPLPC_SRC_FILES})
add_executable(${XPLPC_PROJECT_NAME} "${XPLPC_ALL_FILES}")
else()
message(FATAL_ERROR "Target is invalid: ${XPLPC_TARGET}")
endif()

# xplpc interface include path
Expand All @@ -226,8 +208,17 @@ if (APPLE)
target_link_libraries(${XPLPC_PROJECT_NAME} PRIVATE ${FWK_FOUNDATION})
endif()

# sample
if (XPLPC_BUILD_SAMPLE)
if(XPLPC_TARGET STREQUAL "cxx-static" OR XPLPC_TARGET STREQUAL "cxx-shared")
add_subdirectory(cxx/sample)
elseif(XPLPC_TARGET STREQUAL "c-static" OR XPLPC_TARGET STREQUAL "c-shared")
add_subdirectory(c/sample)
endif()
endif()

# tests
if(XPLPC_ENABLE_TESTS)
if(XPLPC_BUILD_TESTS)
if(XPLPC_TARGET STREQUAL "cxx-static" OR XPLPC_TARGET STREQUAL "cxx-shared")
enable_testing()
add_subdirectory(cxx/tests)
Expand Down
8 changes: 6 additions & 2 deletions c/sample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# files
set(XPLPC_SRC_FILES ${XPLPC_SRC_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp" PARENT_SCOPE)
# sample
set(XPLPC_SAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
add_executable(${XPLPC_PROJECT_NAME}-sample "${XPLPC_SAMPLE_FILES}")

target_link_libraries(${XPLPC_PROJECT_NAME}-sample PRIVATE ${XPLPC_PROJECT_NAME})
target_include_directories(${XPLPC_PROJECT_NAME}-sample PRIVATE c/lib/include)
1 change: 1 addition & 0 deletions c/sample/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// add imports
#include "xplpc/c/platform.h"
#include "xplpc/xplpc.hpp"

#include <iostream>
#include <string>

Expand Down
2 changes: 1 addition & 1 deletion cmake/conan/conan-dependency.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (XPLPC_ENABLE_SERIALIZER_FOR_JSON)
endif()

# test
if(XPLPC_ENABLE_TESTS)
if(XPLPC_BUILD_TESTS)
if(${XPLPC_TARGET} MATCHES "^(cxx|c)-(static|shared)$")
find_package(GTest REQUIRED CONFIG)
target_link_libraries(${XPLPC_PROJECT_NAME}-tests PRIVATE GTest::gtest GTest::gtest_main)
Expand Down
2 changes: 1 addition & 1 deletion cmake/cpm/cpm-dependency.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if(XPLPC_ENABLE_SERIALIZER_FOR_JSON)
endif()

# test
if(XPLPC_ENABLE_TESTS)
if(XPLPC_BUILD_TESTS)
if("${XPLPC_TARGET}" MATCHES "^(cxx|c)-(static|shared)$")
CPMAddPackage("gh:google/googletest@1.13.0")

Expand Down
6 changes: 3 additions & 3 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class TargetConan(ConanFile):
options = {
"shared": [True, False],
"fPIC": [True, False],
"xplpc_enable_tests": [True, False],
"xplpc_enable_serializer_for_json": [True, False],
"xplpc_build_tests": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"xplpc_enable_tests": False,
"xplpc_build_tests": False,
"xplpc_enable_serializer_for_json": True,
}
generators = "CMakeDeps", "CMakeToolchain"
Expand All @@ -30,5 +30,5 @@ def requirements(self):
if self.options.get_safe("xplpc_enable_serializer_for_json"):
self.requires("nlohmann_json/3.11.2")

if self.options.get_safe("xplpc_enable_tests"):
if self.options.get_safe("xplpc_build_tests"):
self.requires("gtest/1.13.0")
31 changes: 17 additions & 14 deletions core/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_task_build_static():
target_data=target_data,
build_folder=target,
has_tests=False,
has_samples=False,
has_sample=False,
has_pic=False,
has_custom_data=True,
)
Expand Down Expand Up @@ -88,7 +88,7 @@ def run_task_build_shared():
target_data=target_data,
build_folder=target,
has_tests=False,
has_samples=False,
has_sample=False,
has_pic=True,
has_custom_data=True,
)
Expand Down Expand Up @@ -131,7 +131,7 @@ def run_task_build_sample():
target_data=target_data,
build_folder="c-sample",
has_tests=False,
has_samples=True,
has_sample=True,
has_pic=False,
has_custom_data=False,
)
Expand All @@ -147,8 +147,9 @@ def run_task_run_sample():

target_data = get_target_data_for_platform()
arch = target_data[0]["arch"]
bin_dir = os.path.join(build_dir, arch, "c", "sample", "bin")

r.run([util.run_name("xplpc")], cwd=os.path.join(build_dir, arch, "bin"))
r.run([util.run_name("xplpc-sample")], cwd=bin_dir)

l.ok()

Expand Down Expand Up @@ -187,7 +188,7 @@ def run_task_build_leaks():
target_data=target_data,
build_folder="c-leaks",
has_tests=False,
has_samples=True,
has_sample=True,
has_pic=False,
has_custom_data=False,
)
Expand All @@ -208,8 +209,10 @@ def run_task_build_leaks():
"build",
"c-leaks",
arch,
"c",
"sample",
"bin",
util.exec_name("xplpc"),
util.exec_name("xplpc-sample"),
),
]
)
Expand Down Expand Up @@ -252,7 +255,7 @@ def run_task_test():
target_data=target_data,
build_folder="c-test",
has_tests=True,
has_samples=False,
has_sample=False,
has_pic=False,
has_custom_data=True,
)
Expand Down Expand Up @@ -318,7 +321,7 @@ def do_build(
target_data,
build_folder,
has_tests,
has_samples,
has_sample,
has_pic,
has_custom_data,
):
Expand Down Expand Up @@ -362,7 +365,7 @@ def do_build(

if has_tests:
run_args.append("-o:h")
run_args.append("xplpc_enable_tests=True")
run_args.append("xplpc_build_tests=True")

run_args.append("--build=missing")
run_args.append("--update")
Expand Down Expand Up @@ -396,15 +399,15 @@ def do_build(

# tests
if has_tests:
run_args.append("-DXPLPC_ENABLE_TESTS=ON")
run_args.append("-DXPLPC_BUILD_TESTS=ON")
else:
run_args.append("-DXPLPC_ENABLE_TESTS=OFF")
run_args.append("-DXPLPC_BUILD_TESTS=OFF")

# sample
if has_samples:
run_args.append("-DXPLPC_ENABLE_SAMPLES=ON")
if has_sample:
run_args.append("-DXPLPC_BUILD_SAMPLE=ON")
else:
run_args.append("-DXPLPC_ENABLE_SAMPLES=OFF")
run_args.append("-DXPLPC_BUILD_SAMPLE=OFF")

# pic
if has_pic:
Expand Down
Loading

0 comments on commit eb4ccff

Please sign in to comment.