Skip to content

Commit c3924b5

Browse files
authored
Addidtion of Findneural-fortran.cmake (#178)
* Addition of Findneural-fortran.cmake * Rename libneural to libneural-fortran * Adapt test/CmakeLists.txt * adapt example/CMakeLists.txt
1 parent f7b6006 commit c3924b5

File tree

5 files changed

+202
-6
lines changed

5 files changed

+202
-6
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ include(cmake/functional.cmake)
2121
include(cmake/h5fortran.cmake)
2222
include(cmake/json.cmake)
2323

24-
# library to archive (libneural.a)
25-
add_library(neural
24+
# library to archive (libneural-fortran.a)
25+
add_library(neural-fortran
2626
src/nf.f90
2727
src/nf/nf_activation.f90
2828
src/nf/nf_base_layer.f90
@@ -64,14 +64,14 @@ add_library(neural
6464
src/nf/io/nf_io_hdf5_submodule.f90
6565
)
6666

67-
target_link_libraries(neural PRIVATE
67+
target_link_libraries(neural-fortran PRIVATE
6868
functional::functional
6969
h5fortran::h5fortran
7070
HDF5::HDF5
7171
jsonfortran::jsonfortran
7272
)
7373

74-
install(TARGETS neural)
74+
install(TARGETS neural-fortran)
7575

7676
# Remove leading or trailing whitespace
7777
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ ctest
203203

204204
to run the tests.
205205

206+
## Using neural-fortran in your project
207+
208+
You can use the CMake module available [here](cmake/Findneural-fortran.cmake) to
209+
find or fetch an installation of this project while configuring your project. This
210+
module makes sure that the `neural-fortran::neural-fortran` target is always generated regardless
211+
of how the neural-fortran is included in the project.
212+
213+
You can configure neural-fortran by setting the appropriate options before
214+
including the subproject.
215+
216+
The following should be added in the CMake file of your directory:
217+
```cmake
218+
if(NOT TARGET "neural-fortran::neural-fortran")
219+
find_package("neural-fortran" REQUIRED)
220+
endif()
221+
```
222+
206223
## Examples
207224

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

cmake/Findneural-fortran.cmake

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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")
176+
endif()
177+
unset(_lib)
178+
unset(_pkg)
179+
unset(_url)

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ foreach(execid
1111
)
1212
add_executable(${execid} ${execid}.f90)
1313
target_link_libraries(${execid} PRIVATE
14-
neural
14+
neural-fortran
1515
h5fortran::h5fortran
1616
jsonfortran::jsonfortran
1717
${LIBS}

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ foreach(execid
1919
loss
2020
)
2121
add_executable(test_${execid} test_${execid}.f90)
22-
target_link_libraries(test_${execid} PRIVATE neural h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
22+
target_link_libraries(test_${execid} PRIVATE neural-fortran h5fortran::h5fortran jsonfortran::jsonfortran ${LIBS})
2323

2424
add_test(NAME test_${execid} COMMAND test_${execid})
2525
endforeach()

0 commit comments

Comments
 (0)