44include (cth_assertions )
55
66#[[ .rst:
7- .. command:: cth_find_program
7+ .. command:: cth_find_optional_program
88
99 .. code-block:: cmake
1010
11- cth_find_program (<out_var> <prog> [args...])
11+ cth_find_optional_program (<out_var> <prog> [args...])
1212
1313 Locates an external program and exports its path to the parent scope.
1414
@@ -18,12 +18,43 @@ include(cth_assertions)
1818 :param args: Additional arguments to pass to find_program (e.g., PATHS, HINTS)
1919 :type args: optional arguments
2020
21- :post: <OUT_VAR> variable is set in PARENT_SCOPE with the full path to the program, or configuration terminates with FATAL_ERROR if not found
21+ :post: <OUT_VAR> variable is set in PARENT_SCOPE with the full path to the program if found, or an empty string if not found
22+
23+ .. note::
24+ Unlike ``cth_find_program()``, this function does not error if the program is not found.
25+ Check if the result variable is empty to determine if the program was found.
26+
2227#]]
23- function (cth_find_program OUT_VAR prog )
28+ function (cth_find_optional_program OUT_VAR prog )
2429
2530 find_program (${OUT_VAR} "${prog} " ${ARGN} )
2631
32+ set (${OUT_VAR} "${${OUT_VAR} }" PARENT_SCOPE )
33+ endfunction ()
34+
35+ #[[ .rst:
36+ .. command:: cth_find_program
37+
38+ .. code-block:: cmake
39+
40+ cth_find_program(<out_var> <prog> [args...])
41+
42+ Locates a required external program and exports its path to the parent scope.
43+ Terminates configuration with FATAL_ERROR if not found.
44+
45+ :param OUT_VAR variable to export program path to
46+ :param prog: Name of the program to find
47+ :param args: Additional arguments to pass to find_program
48+
49+ :post: <OUT_VAR> variable is set in PARENT_SCOPE with the full path to the program, or configuration terminates with FATAL_ERROR if not found
50+
51+ .. seealso::
52+ See ``cth_find_optional_program()`` for a variant that does not error if the program is not found.
53+
54+ #]]
55+ function (cth_find_program OUT_VAR prog )
56+ cth_find_optional_program (${OUT_VAR} "${prog} " ${ARGN} )
57+
2758 cth_assert_true (${OUT_VAR} REASON "Program '${prog} ' not found" )
2859
2960 set (${OUT_VAR} "${${OUT_VAR} }" PARENT_SCOPE )
@@ -63,31 +94,55 @@ function(cth_enable_build_cache)
6394endfunction ()
6495
6596#[[ .rst:
66- .. command:: cth_find_clang_format
97+ .. command:: cth_find_opt_clang_format
6798
6899 .. code-block:: cmake
69100
70- cth_find_clang_format ()
101+ cth_find_opt_clang_format ()
71102
72103 Locates the clang-format executable and exports its path to the parent scope.
104+ Does not error if clang-format is not found.
73105
74- :post: CLANG_FORMAT_EXECUTABLE is set in PARENT_SCOPE with the full path to clang-format, or configuration terminates with FATAL_ERROR if not found
106+ :post: CLANG_FORMAT_EXECUTABLE is set in PARENT_SCOPE with the full path to clang-format, or an empty string if not found
75107
76108 .. note::
77- The clang-format executable must be available in PATH .
109+ Check if CLANG_FORMAT_EXECUTABLE is empty to determine if clang-format was found .
78110
79- .. warning::
80- This function will fail with FATAL_ERROR if clang-format is not found.
81- Ensure clang-format is installed and available in your system PATH.
111+ .. seealso::
112+ Use ``cth_add_clang_format_target()`` from cth_target_utilities to create a format target.
113+
114+ #]]
115+ function (cth_find_opt_clang_format )
116+ cth_find_optional_program (CLANG_FORMAT_EXECUTABLE clang-format )
117+
118+ if (CLANG_FORMAT_EXECUTABLE)
119+ message (STATUS "Found external clang-format: ${CLANG_FORMAT_EXECUTABLE} " )
120+ endif ()
121+
122+ set (CLANG_FORMAT_EXECUTABLE ${CLANG_FORMAT_EXECUTABLE} PARENT_SCOPE )
123+ endfunction ()
124+
125+ #[[ .rst:
126+ .. command:: cth_find_clang_format
127+
128+ .. code-block:: cmake
129+
130+ cth_find_clang_format()
131+
132+ Locates a required clang-format executable and exports its path to the parent scope.
133+ Terminates configuration with FATAL_ERROR if not found.
134+
135+ :post: CLANG_FORMAT_EXECUTABLE is set in PARENT_SCOPE with the full path to clang-format, or configuration terminates with FATAL_ERROR
82136
83137 .. seealso::
138+ See ``cth_find_opt_clang_format()`` for a variant that does not error if clang-format is not found.
84139 Use ``cth_add_clang_format_target()`` from cth_target_utilities to create a format target.
85140
86141#]]
87142function (cth_find_clang_format )
88- cth_find_program (CLANG_FORMAT_EXECUTABLE clang-format )
143+ cth_find_opt_clang_format ()
144+
145+ cth_assert_true (CLANG_FORMAT_EXECUTABLE REASON "clang-format not found" )
89146
90- message (STATUS "Found external clang-format: ${CLANG_FORMAT_EXECUTABLE} " )
91-
92147 set (CLANG_FORMAT_EXECUTABLE ${CLANG_FORMAT_EXECUTABLE} PARENT_SCOPE )
93148endfunction ()
0 commit comments