| 1 |
cmake_minimum_required(VERSION 3.16.0 FATAL_ERROR) |
| 2 |
project(cppgir_example VERSION 2.0.0) |
| 3 |
|
| 4 |
# following example uses the Gio-2.0 gir |
| 5 |
# will generate code for that |
| 6 |
|
| 7 |
# locate cppgir |
| 8 |
|
| 9 |
# NOTE recent cmake version have more options and automagic find_package integration |
| 10 |
# (see https://cmake.org/cmake/help/latest/guide/using-dependencies/index.html |
| 11 |
# or https://cmake.org/cmake/help/latest/module/FetchContent.html) |
| 12 |
# but a slightly older approach is used here |
| 13 |
find_package(cppgir ${CMAKE_PROJECT_VERSION} QUIET) |
| 14 |
if (cppgir_FOUND) |
| 15 |
# ok |
| 16 |
message(STATUS "using system cppgir") |
| 17 |
elseif (CMAKE_VERSION GREATER_EQUAL 3.22) |
| 18 |
# older version fail processing the subdirectory below |
| 19 |
message(STATUS "cppgir not found, fetching instead") |
| 20 |
# of course, alternatively, |
| 21 |
# use any other method to include cppgir (e.g. git submodule) |
| 22 |
# which can also be done unconditionally (without trying find_package first) |
| 23 |
include(FetchContent) |
| 24 |
FetchContent_Declare( |
| 25 |
cppgir |
| 26 |
GIT_REPOSITORY https://gitlab.com/mnauw/cppgir.git |
| 27 |
GIT_TAG master |
| 28 |
GIT_SHALLOW TRUE |
| 29 |
GIT_SUBMODULES_RECURSE TRUE |
| 30 |
# disable auto-invoke of add_subdirectory() below |
| 31 |
SOURCE_SUBDIR data |
| 32 |
) |
| 33 |
# no CMakeLists in specified subdir, so we handle it explicitly |
| 34 |
FetchContent_MakeAvailable(cppgir) |
| 35 |
set(CPPGIR_DIR "${cppgir_SOURCE_DIR}") |
| 36 |
else () |
| 37 |
# obviously, actual mileage/location may vary (e.g. using git submodule) |
| 38 |
set(CPPGIR_DIR "../..") |
| 39 |
endif () |
| 40 |
|
| 41 |
if (CPPGIR_DIR) |
| 42 |
# adjust options |
| 43 |
set(BUILD_TESTING OFF) |
| 44 |
set(BUILD_EXAMPLES OFF) |
| 45 |
set(BUILD_EMBED_IGNORE ON) |
| 46 |
add_subdirectory(${CPPGIR_DIR} cppgir EXCLUDE_FROM_ALL) |
| 47 |
endif () |
| 48 |
|
| 49 |
set(EXAMPLE_NS "Gio-2.0") |
| 50 |
# of course, can be anywhere, e.g. inside/outside the build directory |
| 51 |
# set(GENERATED_DIR "/tmp/gi.example") |
| 52 |
set(GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") |
| 53 |
add_custom_command(OUTPUT ${GENERATED_DIR} |
| 54 |
COMMENT "Generating wrapper code for: ${EXAMPLE_NS}" |
| 55 |
DEPENDS CppGir::cppgir |
| 56 |
COMMAND CppGir::cppgir --output ${GENERATED_DIR} ${EXAMPLE_NS} |
| 57 |
COMMAND cmake -E touch_nocreate ${GENERATED_DIR} |
| 58 |
) |
| 59 |
add_custom_target(generate DEPENDS ${GENERATED_DIR}) |
| 60 |
|
| 61 |
# need the code and libs and especially GIRs |
| 62 |
include(FindPkgConfig) |
| 63 |
pkg_check_modules(GOBJECT REQUIRED IMPORTED_TARGET gobject-2.0) |
| 64 |
pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0 gio-unix-2.0) |
| 65 |
|
| 66 |
# (actually) needs no generated code, but it needs basic libs |
| 67 |
add_executable(ext-gobject src/ext-gobject.cpp) |
| 68 |
target_link_libraries(ext-gobject PRIVATE CppGir::gi PkgConfig::GOBJECT) |
| 69 |
|
| 70 |
# (really) needs generated code |
| 71 |
add_executable(ext-gio src/ext-gio.cpp) |
| 72 |
target_link_libraries(ext-gio PRIVATE CppGir::gi PkgConfig::GIO) |
| 73 |
target_include_directories(ext-gio PRIVATE ${GENERATED_DIR}) |
| 74 |
add_dependencies(ext-gio generate) |