Skip to content

Commit bbe984b

Browse files
authored
Example using the library via FetchContent (#6)
1 parent 0085ade commit bbe984b

6 files changed

Lines changed: 82 additions & 2 deletions

File tree

example/simple/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ gt_add_bindings_library(simple SOURCES simple.cpp)
1010
# 3) link the library to a fortran executable
1111
add_executable(driver driver.f90)
1212
target_link_libraries(driver simple_fortran)
13+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
simple.f90
2+
simple.h
3+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.12.4) #TODO replace with generated version
2+
project(simple_example_with_fetch_content LANGUAGES CXX Fortran)
3+
4+
# 1) fetch c_bindings from the repository
5+
# TODO: use FetchContent_MakeAvailable() once we require CMake 3.14+
6+
include(FetchContent)
7+
FetchContent_Declare(
8+
c_bindings
9+
# In your application remove SOURCE_DIR ...
10+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..
11+
# By the following 2 lines to fetch content directly from github
12+
# GIT_REPOSITORY https://github.com/GridTools/c_bindings.git
13+
# GIT_TAG master # consider replacing master by a tagged version
14+
)
15+
FetchContent_GetProperties(c_bindings)
16+
if(NOT c_bindings_POPULATED)
17+
FetchContent_Populate(c_bindings)
18+
add_subdirectory(${c_bindings_SOURCE_DIR} ${c_bindings_BINARY_DIR})
19+
endif()
20+
21+
# 2) create a library with bindings. This will generate the files simple.h and simple.f90 which can be included within C and Fortran. In CMake you can use them by linking against `simple_c`or `simple_fortran`.
22+
gt_add_bindings_library(simple SOURCES simple.cpp)
23+
24+
# 3) link the generated library to a fortran executable
25+
add_executable(driver driver.f90)
26+
target_link_libraries(driver simple_fortran)
27+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
! GridTools
2+
!
3+
! Copyright (c) 2014-2019, ETH Zurich
4+
! All rights reserved.
5+
!
6+
! Please, refer to the LICENSE file in the root directory.
7+
! SPDX-License-Identifier: BSD-3-Clause
8+
9+
program main
10+
use iso_c_binding
11+
use gt_handle
12+
use simple
13+
implicit none
14+
integer, parameter :: i = 9
15+
16+
call print_number_from_cpp(i)
17+
18+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* GridTools
3+
*
4+
* Copyright (c) 2014-2019, ETH Zurich
5+
* All rights reserved.
6+
*
7+
* Please, refer to the LICENSE file in the root directory.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#include <c_bindings/export.hpp>
12+
#include <iostream>
13+
14+
// In this example, we demonstrate how the c_bindings library can be used to export functions to C and Fortran.
15+
16+
namespace {
17+
void print_number(int i) {
18+
std::cout << "Printing from C++: " << i << std::endl;
19+
}
20+
21+
// Exports a unary function with the name `print_number_from_cpp`, which forwards to `print_number`.
22+
GT_EXPORT_BINDING_1(print_number_from_cpp, print_number);
23+
} // namespace

jenkins/build.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ cwd=$(pwd)
3838
# install c_bindings
3939
mkdir -p build && cd build
4040
cmake .. -DCMAKE_INSTALL_PREFIX=${cwd}/install
41-
nice -15 make -j8 install
41+
nice make -j8 install
4242
ctest .
4343

4444
# compile examples using the installation
45+
# simple example using installation
4546
cd ${cwd}/example/simple
4647
mkdir -p build && cd build
4748
cmake .. -Dgt_c_bindings_DIR=${cwd}/install/lib/cmake
48-
nice -15 make -j8
49+
nice make -j8
50+
./driver
51+
52+
# simple example using FetchContent
53+
cd ${cwd}/example/simple_fetch_content
54+
mkdir -p build && cd build
55+
cmake ..
56+
nice make -j8
4957
./driver
5058

0 commit comments

Comments
 (0)