Skip to content

Commit

Permalink
Update CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Sep 13, 2024
1 parent 6db1422 commit 42ec0bd
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ jobs:
- name: Build
run: cmake --build ${{github.workspace}}/build

- name: Install
working-directory: ${{github.workspace}}/build
run: sudo cmake --install .

- name: Test Installation
run: |
cmake -GNinja -S test/test_install/ -B build/asbind20_test_install -DCMAKE_TOOLCHAIN_FILE="${VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake"
cmake --build build/asbind20_test_install
build/asbind20_test_install/asbind20_test_install
- name: Test
working-directory: ${{github.workspace}}/build
run: ctest
73 changes: 70 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(STRINGS "include/asbind20/asbind.hpp" asbind20_version_major_line REGEX "^#define ASBIND20_VERSION_MAJOR [0-9]+$")
file(STRINGS "include/asbind20/asbind.hpp" asbind20_version_minor_line REGEX "^#define ASBIND20_VERSION_MINOR [0-9]+$")
file(STRINGS "include/asbind20/asbind.hpp" asbind20_version_patch_line REGEX "^#define ASBIND20_VERSION_PATCH [0-9]+$")
string(REGEX REPLACE "^#define ASBIND20_VERSION_MAJOR ([0-9]+)$" "\\1" asbind20_version_major "${asbind20_version_major_line}")
string(REGEX REPLACE "^#define ASBIND20_VERSION_MINOR ([0-9]+)$" "\\1" asbind20_version_minor "${asbind20_version_minor_line}")
string(REGEX REPLACE "^#define ASBIND20_VERSION_PATCH ([0-9]+)$" "\\1" asbind20_version_patch "${asbind20_version_patch_line}")
unset(asbind20_version_major_line)
unset(asbind20_version_minor_line)
unset(asbind20_version_patch_line)

message("asbind20 v${asbind20_version_major}.${asbind20_version_minor}.${asbind20_version_patch}")

project(
asbind20
VERSION 0.1.0
VERSION ${asbind20_version_major}.${asbind20_version_minor}.${asbind20_version_patch}
LANGUAGES CXX
)

find_package(Threads REQUIRED)
find_package(Angelscript 2.37.0 CONFIG REQUIRED)

add_library(asbind20 STATIC)

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

file(GLOB asbind20_src "include/*.hpp" "src/*.cpp")
option(asbind_build_ext "Build extensions" ON)
option(asbind_build_ext_array "Build array extension" ON)
Expand All @@ -24,24 +42,73 @@ option(asbind_build_ext_assert "Build assert extension" ON)
if(${asbind_build_ext})
if(${asbind_build_ext_array})
list(APPEND asbind20_src "src/ext/array.cpp")
target_compile_definitions(asbind20 PUBLIC ASBIND20_EXT_ARRAY=1)
endif()
if(${asbind_build_ext_math})
list(APPEND asbind20_src "src/ext/math.cpp")
target_compile_definitions(asbind20 PUBLIC ASBIND20_EXT_MATH=1)
endif()
if(${asbind_build_ext_helper})
list(APPEND asbind20_src "src/ext/helper.cpp")
target_compile_definitions(asbind20 PUBLIC ASBIND20_EXT_HELPER=1)
endif()
if(${asbind_build_ext_stdstring})
list(APPEND asbind20_src "src/ext/stdstring.cpp")
target_compile_definitions(asbind20 PUBLIC ASBIND20_EXT_STDSTRING=1)
endif()
if(${asbind_build_ext_assert})
list(APPEND asbind20_src "src/ext/assert.cpp")
target_compile_definitions(asbind20 PUBLIC ASBIND20_EXT_ASSERT=1)
endif()
endif()

add_library(asbind20 ${asbind20_src})
target_include_directories(asbind20 PUBLIC include)
include(GNUInstallDirs)

target_sources(asbind20 PRIVATE ${asbind20_src})
target_include_directories(asbind20 PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(asbind20 PUBLIC Threads::Threads)
target_link_libraries(asbind20 PUBLIC Angelscript::angelscript)
target_compile_definitions(asbind20
PRIVATE $<$<CONFIG:Debug>:ASBIND20_DEBUG=1>
)

install(
TARGETS asbind20
EXPORT asbind20Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.inl"
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"asbind20ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/cmake/asbind20Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/asbind20Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/asbind20
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/asbind20Config.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/asbind20
)
install(
EXPORT asbind20Targets
FILE asbind20Targets.cmake
NAMESPACE asbind20::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/asbind20
)

option(asbind_build_test "Build tests" OFF)
mark_as_advanced(asbind_build_test)
Expand Down
7 changes: 7 additions & 0 deletions cmake/asbind20Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PACKAGE_INIT@

find_package(Threads REQUIRED)
find_package(Angelscript 2.37.0 CONFIG REQUIRED)

include("${CMAKE_CURRENT_LIST_DIR}/asbind20Targets.cmake")
check_required_components(asbind20)
6 changes: 6 additions & 0 deletions example/asexec/asexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ int main(int argc, char* argv[])
<< "asGetLibraryVersion: " << asGetLibraryVersion()
<< '\n'
<< "asGetLibraryOptions: " << asGetLibraryOptions()
<< '\n'
<< "asbind20::library_version: " << asbind20::library_version()
<< '\n'
<< "asbind20::library_options: " << asbind20::library_options()
<< std::endl;
return 0;
}

asIScriptEngine* engine = asCreateScriptEngine();
engine->SetMessageCallback(asFUNCTION(message_callback), nullptr, asCALL_CDECL);

engine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, true);

asbind20::ext::register_script_array(engine);
asbind20::ext::register_math_function(engine);
asbind20::ext::register_std_string(engine);
Expand Down
19 changes: 19 additions & 0 deletions include/asbind20/asbind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@

#pragma once

// clang-format off: Used by CMakeLists.txt for parsing version

#define ASBIND20_VERSION_MAJOR 0
#define ASBIND20_VERSION_MINOR 1
#define ASBIND20_VERSION_PATCH 0

// clang-format on

#define ASBIND20_VERSION_STRING "0.1.0"

#include "utility.hpp"
#include "bind.hpp"
#include "invoke.hpp"
#include "builder.hpp"

namespace asbind20
{
[[nodiscard]]
const char* library_version() noexcept;

[[nodiscard]]
const char* library_options() noexcept;
} // namespace asbind20

#endif
38 changes: 38 additions & 0 deletions src/asbind20.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
#include <asbind20/asbind.hpp>

namespace asbind20
{
[[nodiscard]]
const char* library_version() noexcept
{
#ifdef ASBIND20_DEBUG
return ASBIND20_VERSION_STRING " DEBUG";
#else
return ASBIND20_VERSION_STRING;
#endif
}

const char* library_options() noexcept
{
const char* str = " "

// Extensions
#ifdef ASBIND20_EXT_ARRAY
"ASBIND20_EXT_ARRAY "
#endif
#ifdef ASBIND20_EXT_STDSTRING
"ASBIND20_EXT_STDSTRING "
#endif
#ifdef ASBIND20_EXT_MATH
"ASBIND20_EXT_MATH "
#endif
#ifdef ASBIND20_EXT_ASSERT
"ASBIND20_EXT_ASSERT "
#endif
#ifdef ASBIND20_EXT_HELPER
"ASBIND20_EXT_HELPER "
#endif
;

return str;
}
} // namespace asbind20
2 changes: 2 additions & 0 deletions test/test_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,7 @@ int main(int argc, char* argv[])
::testing::InitGoogleTest(&argc, argv);
std::cerr << "asGetLibraryVersion(): " << asGetLibraryVersion() << std::endl;
std::cerr << "asGetLibraryOptions(): " << asGetLibraryOptions() << std::endl;
std::cerr << "asbind20::library_version(): " << asbind20::library_version() << std::endl;
std::cerr << "asbind20::library_options(): " << asbind20::library_options() << std::endl;
return RUN_ALL_TESTS();
}
11 changes: 11 additions & 0 deletions test/test_install/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED)

project(asbind20_test_install LANGUAGES CXX)

find_package(asbind20 REQUIRED)

add_executable(asbind20_test_install "test_install.cpp")
target_link_libraries(asbind20_test_install PRIVATE asbind20::asbind20)
13 changes: 13 additions & 0 deletions test/test_install/test_install.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <asbind20/asbind.hpp>

int main()
{
std::cout
<< asbind20::library_version()
<< '\n'
<< asbind20::library_options()
<< std::endl;

return 0;
}

0 comments on commit 42ec0bd

Please sign in to comment.