Skip to content

Commit

Permalink
Add support for specifying CMake targets
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanBryan51 committed Mar 4, 2024
1 parent 2c603fb commit 9cc7be6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ add_executable(
)
target_compile_options(cable PRIVATE ${CABLE_Fortran_FLAGS})
target_link_libraries(cable PRIVATE PkgConfig::NETCDF)
install(TARGETS cable RUNTIME)
install(TARGETS cable RUNTIME OPTIONAL)

if(MPI_Fortran_FOUND)
add_executable(
Expand All @@ -159,5 +159,5 @@ if(MPI_Fortran_FOUND)
)
target_compile_options(cable-mpi PRIVATE ${CABLE_Fortran_FLAGS})
target_link_libraries(cable-mpi PRIVATE PkgConfig::NETCDF MPI::MPI_Fortran)
install(TARGETS cable-mpi RUNTIME)
install(TARGETS cable-mpi RUNTIME OPTIONAL)
endif()
18 changes: 17 additions & 1 deletion build.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

nproc_default=4
targets_default=(all)

script_name=$(basename "${0}")

Expand All @@ -17,6 +18,10 @@ Options:
Specify the compiler to use.
-j <jobs> Specify the number of parallel jobs in the compilation. By
default this value is set to $nproc_default.
-t <tgt>, --target <tgt>
Build the CMake target <tgt> instead of the default target
(${targets_default[*]}). This option may be used more than once
to specify multiple targets.
-h, --help Show this screen.
Enabling debug mode:
Expand All @@ -29,6 +34,12 @@ Enabling verbose output from Makefile builds:
To enable more verbose output from Makefile builds, specify the CMake option
-DCMAKE_VERBOSE_MAKEFILE=ON when invoking $script_name.
Enabling serial only or MPI only builds:
Serial only or MPI only builds can be done by specifying the --target
<executable> option when invoking $script_name where <executable> is the name
of either the serial or MPI executable.
EOF
}

Expand All @@ -48,6 +59,10 @@ while [ $# -gt 0 ]; do
CMAKE_BUILD_PARALLEL_LEVEL=$2
shift
;;
-t|--target)
targets+=("$2")
shift
;;
-h|--help)
show_help
exit
Expand Down Expand Up @@ -88,7 +103,8 @@ if hostname -f | grep gadi.nci.org.au > /dev/null; then
fi

export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:=$nproc_default}"
: "${targets:=${targets_default[*]}}"

cmake -S . -B build "${cmake_args[@]}" &&\
cmake --build build &&\
cmake --build build --target "${targets[@]}" &&\
cmake --install build --prefix .
8 changes: 4 additions & 4 deletions documentation/docs/user_guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ To execute the build, run:

./build.bash

This loads the required build dependencies and invokes CMake. Both serial and parallel (MPI) applications are built with a single invocation of CMake. **Note** the MPI executable will only be built if CMake can find the location of the installed MPI libraries.
This loads the required build dependencies and invokes CMake. Both serial and parallel (MPI) applications are built by default with a single invocation of CMake. **Note** the MPI executable will only be built if CMake can find the location of the installed MPI libraries.

### Cleaning the build

Expand All @@ -106,9 +106,9 @@ The release build is default. To enable debug mode, specify the CMake option `-D

To enable more verbose output from Makefile builds, specify the CMake option `-DCMAKE_VERBOSE_MAKEFILE=ON` when invoking `build.bash`.

### Parallel compilation
### Enabling serial only or MPI only builds:

By default, the compilation is done in parallel. The number of parallel jobs can be overwritten by setting the environment variable [`CMAKE_BUILD_PARALLEL_LEVEL`](https://cmake.org/cmake/help/latest/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html).
Serial only or MPI only builds can be done by specifying the `--target <executable>` option when invoking `build.bash` where `<executable>` is the name of either the serial or MPI executable.

???+ tip
Run `./build.bash --help` for information on supported options.
Expand All @@ -120,4 +120,4 @@ By default, the compilation is done in parallel. The number of parallel jobs can
[build.bash-hostname-check]: https://github.com/CABLE-LSM/CABLE/blob/main/build.bash#L45-L55
[clean-build]: installation.md/#cleaning-the-build
[build-system]: ../developer_guide/other_resources/build_system.md
[hive-forum-cable]: https://forum.access-hive.org.au/c/land/cable/18
[hive-forum-cable]: https://forum.access-hive.org.au/c/land/cable/18

0 comments on commit 9cc7be6

Please sign in to comment.