Skip to content

Commit

Permalink
Support add_subdirectory()
Browse files Browse the repository at this point in the history
1. Update the main CMakeLists.txt
2. Add test project
  • Loading branch information
HenryAWE committed May 21, 2024
1 parent debdac6 commit 55623b1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ jobs:
cmake --build build/papilio_test_install
build/papilio_test_install/papilio_test_install
- name: Test add_subdirectory
run: |
cmake -GNinja -S test/test_subdir/ -B build/papilio_test_subdir -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++-12
cmake --build build/papilio_test_subdir
build/papilio_test_subdir/papilio_test_subdir
Clang:
runs-on: ubuntu-22.04
strategy:
Expand Down
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ endif()
include(GNUInstallDirs)

add_library(papilio ${papilio_src})
target_sources(papilio PUBLIC "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/natvis/papilio.natvis>")
target_sources(papilio PUBLIC "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/cpp.hint>")
target_sources(papilio PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/natvis/papilio.natvis>")
target_sources(papilio PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/cpp.hint>")
target_include_directories(papilio PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# Add an ALIAS target for using the library by add_subdirectory()
add_library(papilio::papilio ALIAS papilio)

# Enable warnings for development
target_compile_options(papilio PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
Expand Down
15 changes: 15 additions & 0 deletions test/test_subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED)

project(papilio_test_subdir LANGUAGES CXX)

add_subdirectory(
../..
# Adding a parent directory as a subdirectory needs to specify its binary_dir
${PROJECT_BINARY_DIR}/papilio
)

add_executable(papilio_test_subdir "test_subdir.cpp")
target_link_libraries(papilio_test_subdir PRIVATE papilio::papilio)
10 changes: 10 additions & 0 deletions test/test_subdir/test_subdir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <papilio/papilio.hpp>

int main()
{
papilio::println("papilio as submodule");
papilio::println();
papilio::println("{} {} {}", "hello", 42, 3.14f);

return 0;
}

0 comments on commit 55623b1

Please sign in to comment.