Skip to content

Commit d516437

Browse files
Update for cmake use of neural-fortran (#192)
* Update for cmake use of neural-fortran * Update readme * Fix up comment in cmake file * Add CMakeLists.txt to CI * Update cmake * Remove -DSERIAL=1 * Remove -DSERIAL=1 --------- Co-authored-by: Milan Curcic <[email protected]>
1 parent 9a40117 commit d516437

File tree

4 files changed

+67
-186
lines changed

4 files changed

+67
-186
lines changed

.github/workflows/ci.yml

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ on:
77
- ".github/workflows/ci.yml"
88
- "fpm.toml"
99
- "**.f90"
10+
- "CMakelists.txt"
1011

1112
pull_request:
1213
paths:
1314
- ".github/workflows/ci.yml"
1415
- "fpm.toml"
1516
- "**.f90"
17+
- "CMakelists.txt"
1618

1719
jobs:
1820

@@ -21,20 +23,24 @@ jobs:
2123
runs-on: ubuntu-latest
2224
steps:
2325
- uses: actions/checkout@v4
26+
- name: CMake Configure
27+
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug
2428
- name: Compile
25-
run: cmake -DCMAKE_BUILD_TYPE=Debug -DSERIAL=1 . && make
29+
run: cmake --build build
2630
- name: Test
27-
run: make test
31+
run: ctest --test-dir build
2832

2933
gnu-cmake-release:
3034
name: gnu-cmake-release
3135
runs-on: ubuntu-latest
3236
steps:
3337
- uses: actions/checkout@v4
38+
- name: CMake Configure
39+
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release
3440
- name: Compile
35-
run: cmake -DCMAKE_BUILD_TYPE=Release -DSERIAL=1 . && make
41+
run: cmake --build build
3642
- name: Test
37-
run: make test
43+
run: ctest --test-dir build
3844

3945
gnu-fpm-debug:
4046
name: gnu-fpm-debug

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,25 @@ add_library(neural-fortran
5454
)
5555

5656
target_link_libraries(neural-fortran PRIVATE)
57+
set_target_properties(neural-fortran PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)
5758

5859
install(TARGETS neural-fortran)
5960

61+
# Install the module files
62+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ DESTINATION include)
63+
64+
# Also install the cmake/Findneural-fortran.cmake file
65+
install(FILES cmake/Findneural-fortran.cmake DESTINATION include/cmake/neural-fortran)
66+
6067
# Remove leading or trailing whitespace
6168
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")
6269

6370
if(${PROJECT_NAME}_BUILD_TESTING)
71+
message(STATUS "Building tests")
6472
add_subdirectory(test)
6573
endif()
6674

6775
if(${PROJECT_NAME}_BUILD_EXAMPLES)
76+
message(STATUS "Building examples")
6877
add_subdirectory(example)
6978
endif()

README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,31 @@ find or fetch an installation of this project while configuring your project. Th
183183
module makes sure that the `neural-fortran::neural-fortran` target is always generated regardless
184184
of how the neural-fortran is included in the project.
185185

186-
You can configure neural-fortran by setting the appropriate options before
187-
including the subproject.
186+
First, either copy `Findneural-fortran.cmake` to, say, your project's `cmake` directory
187+
and then include it in your `CMakeLists.txt` file:
188+
189+
```cmake
190+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
191+
```
192+
or use the `CMAKE_MODULE_PATH` variable to point to the directory where it is installed.
193+
194+
Next you need to set `neural-fortran_ROOT_DIR` to the directory where neural-fortran is installed
195+
such that `neural-fortran_ROOT_DIR/lib/libneural-fortran.a` exists.
188196

189197
The following should be added in the CMake file of your directory:
190198

191199
```cmake
192-
if(NOT TARGET "neural-fortran::neural-fortran")
193-
find_package("neural-fortran" REQUIRED)
200+
if(NOT TARGET neural-fortran::neural-fortran)
201+
find_package(neural-fortran REQUIRED)
194202
endif()
195203
```
196204

205+
and then to use the target in your project:
206+
207+
```cmake
208+
target_link_libraries(your_target PRIVATE neural-fortran::neural-fortran)
209+
```
210+
197211
## Examples
198212

199213
The easiest way to get a sense of how to use neural-fortran is to look at

cmake/Findneural-fortran.cmake

+30-178
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,31 @@
1-
# SPDX-Identifier: MIT
2-
# Based on https://github.com/fortran-lang/stdlib-cmake-example
3-
4-
#[[.rst:
5-
Find neural-fortran
6-
-------------------
7-
8-
Makes the neural-fortran library project available.
9-
10-
Imported Targets
11-
^^^^^^^^^^^^^^^^
12-
13-
This module provides the following imported target, if found:
14-
15-
``neural-fortran::neural-fortran``
16-
The neural-fortran library
17-
18-
19-
Result Variables
20-
^^^^^^^^^^^^^^^^
21-
22-
This module will define the following variables:
23-
24-
``NEURAL-FORTRAN_FOUND``
25-
True if the neural-fortran library is available
26-
27-
``NEURAL-FORTRAN_SOURCE_DIR``
28-
Path to the source directory of the neural-fortran library project,
29-
only set if the project is included as source.
30-
31-
``NEURAL-FORTRAN_BINARY_DIR``
32-
Path to the binary directory of the neural-fortran library project,
33-
only set if the project is included as source.
34-
35-
Cache variables
36-
^^^^^^^^^^^^^^^
37-
38-
The following cache variables may be set to influence the library detection:
39-
40-
``NEURAL-FORTRAN_FIND_METHOD``
41-
Methods to find or make the project available. Available methods are
42-
- ``cmake``: Try to find via CMake config file
43-
- ``pkgconf``: Try to find via pkg-config file
44-
- ``subproject``: Use source in subprojects directory
45-
- ``fetch``: Fetch the source from upstream
46-
47-
``NEURAL-FORTRAN_DIR``
48-
Used for searching the CMake config file
49-
50-
``NEURAL-FORTRAN_SUBPROJECT``
51-
Directory to find the neural-fortran library subproject, relative to the project root
52-
53-
#]]
54-
55-
set(_lib "neural-fortran")
56-
set(_pkg "NEURAL-FORTRAN")
57-
set(_url "https://github.com/modern-fortran/neural-fortran.git")
58-
59-
if(NOT DEFINED "${_pkg}_FIND_METHOD")
60-
if(DEFINED "${PROJECT_NAME}-dependency-method")
61-
set("${_pkg}_FIND_METHOD" "${${PROJECT_NAME}-dependency-method}")
62-
else()
63-
set("${_pkg}_FIND_METHOD" "cmake" "pkgconf" "subproject" "fetch")
64-
endif()
65-
set("_${_pkg}_FIND_METHOD")
66-
endif()
67-
68-
foreach(method ${${_pkg}_FIND_METHOD})
69-
if(TARGET "${_lib}::${_lib}")
70-
break()
71-
endif()
72-
73-
if("${method}" STREQUAL "cmake")
74-
message(STATUS "${_lib}: Find installed package")
75-
if(DEFINED "${_pkg}_DIR")
76-
set("_${_pkg}_DIR")
77-
set("${_lib}_DIR" "${_pkg}_DIR")
78-
endif()
79-
find_package("${_lib}" CONFIG)
80-
if("${_lib}_FOUND")
81-
message(STATUS "${_lib}: Found installed package")
82-
break()
83-
endif()
84-
endif()
85-
86-
if("${method}" STREQUAL "pkgconf")
87-
find_package(PkgConfig QUIET)
88-
pkg_check_modules("${_pkg}" QUIET "${_lib}")
89-
if("${_pkg}_FOUND")
90-
message(STATUS "Found ${_lib} via pkg-config")
91-
92-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
93-
target_link_libraries(
94-
"${_lib}::${_lib}"
95-
INTERFACE
96-
"${${_pkg}_LINK_LIBRARIES}"
97-
)
98-
target_include_directories(
99-
"${_lib}::${_lib}"
100-
INTERFACE
101-
"${${_pkg}_INCLUDE_DIRS}"
102-
)
103-
104-
break()
105-
endif()
106-
endif()
107-
108-
if("${method}" STREQUAL "subproject")
109-
if(NOT DEFINED "${_pkg}_SUBPROJECT")
110-
set("_${_pkg}_SUBPROJECT")
111-
set("${_pkg}_SUBPROJECT" "subprojects/${_lib}")
112-
endif()
113-
set("${_pkg}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/${${_pkg}_SUBPROJECT}")
114-
set("${_pkg}_BINARY_DIR" "${PROJECT_BINARY_DIR}/${${_pkg}_SUBPROJECT}")
115-
if(EXISTS "${${_pkg}_SOURCE_DIR}/CMakeLists.txt")
116-
message(STATUS "Include ${_lib} from ${${_pkg}_SUBPROJECT}")
117-
add_subdirectory(
118-
"${${_pkg}_SOURCE_DIR}"
119-
"${${_pkg}_BINARY_DIR}"
120-
)
121-
122-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
123-
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
124-
125-
# We need the module directory in the subproject before we finish the configure stage
126-
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/mod_files")
127-
make_directory("${${_pkg}_BINARY_DIR}/mod_files")
128-
endif()
129-
130-
break()
131-
endif()
132-
endif()
133-
134-
if("${method}" STREQUAL "fetch")
135-
message(STATUS "Retrieving ${_lib} from ${_url}")
136-
include(FetchContent)
137-
FetchContent_Declare(
138-
"${_lib}"
139-
GIT_REPOSITORY "${_url}"
140-
GIT_TAG "HEAD"
141-
)
142-
FetchContent_MakeAvailable("${_lib}")
143-
144-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
145-
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
146-
147-
# We need the module directory in the subproject before we finish the configure stage
148-
FetchContent_GetProperties("${_lib}" SOURCE_DIR "${_pkg}_SOURCE_DIR")
149-
FetchContent_GetProperties("${_lib}" BINARY_DIR "${_pkg}_BINARY_DIR")
150-
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/mod_files")
151-
make_directory("${${_pkg}_BINARY_DIR}/mod_files")
152-
endif()
153-
154-
break()
155-
endif()
156-
157-
endforeach()
158-
159-
if(TARGET "${_lib}::${_lib}")
160-
set("${_pkg}_FOUND" TRUE)
161-
else()
162-
set("${_pkg}_FOUND" FALSE)
163-
endif()
164-
165-
if(DEFINED "_${_pkg}_SUBPROJECT")
166-
unset("${_pkg}_SUBPROJECT")
167-
unset("_${_pkg}_SUBPROJECT")
168-
endif()
169-
if(DEFINED "_${_pkg}_DIR")
170-
unset("${_lib}_DIR")
171-
unset("_${_pkg}_DIR")
172-
endif()
173-
if(DEFINED "_${_pkg}_FIND_METHOD")
174-
unset("${_pkg}_FIND_METHOD")
175-
unset("_${_pkg}_FIND_METHOD")
1+
# Find the native neural-fortran includes and library
2+
#
3+
# neural-fortran_INCLUDE_DIRS - where to find nf.mod, etc.
4+
# neural-fortran_LIBRARIES - List of libraries when using neural-fortran.
5+
# neural-fortran_FOUND - True if neural-fortran found.
6+
#
7+
# To use neural-fortran_ROOT_DIR to specify the prefix directory of neural-fortran
8+
9+
10+
find_path(neural-fortran_INCLUDE_DIRS
11+
NAMES nf.mod
12+
HINTS ${neural-fortran_ROOT_DIR}/include ENV neural-fortran_INCLUDE_DIR)
13+
14+
find_library(neural-fortran_LIBRARIES
15+
NAMES neural-fortran
16+
HINTS ${neural-fortran_ROOT_DIR}/lib ENV neural-fortran_LIB_DIR)
17+
18+
include(FindPackageHandleStandardArgs)
19+
find_package_handle_standard_args(neural-fortran DEFAULT_MSG neural-fortran_LIBRARIES neural-fortran_INCLUDE_DIRS)
20+
21+
mark_as_advanced(
22+
neural-fortran_LIBRARIES
23+
neural-fortran_INCLUDE_DIRS)
24+
25+
if(neural-fortran_FOUND AND NOT (TARGET neural-fortran::neural-fortran))
26+
add_library (neural-fortran::neural-fortran STATIC IMPORTED)
27+
set_target_properties(neural-fortran::neural-fortran
28+
PROPERTIES
29+
IMPORTED_LOCATION ${neural-fortran_LIBRARIES}
30+
INTERFACE_INCLUDE_DIRECTORIES ${neural-fortran_INCLUDE_DIRS})
17631
endif()
177-
unset(_lib)
178-
unset(_pkg)
179-
unset(_url)

0 commit comments

Comments
 (0)