Skip to content

Commit e300d1b

Browse files
feat(taskfiles)!: Add cmake:install-deps-and-generate-settings to install all CMake-based dependencies and write their settings files. (#41)
Co-authored-by: kirkrodrigues <[email protected]>
1 parent 20fe6d7 commit e300d1b

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

exports/taskfiles/utils/cmake.yaml

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,29 @@ tasks:
8585
{{- end}}
8686
8787
# Runs the CMake install step for the given build directory. The caller must have previously
88-
# called `build` on `BUILD_DIR` for this task to succeed. We purposely omit `sources` and
89-
# `generates` as we defer to `cmake` to decide whether it should perform any actions.
88+
# called `build` on `BUILD_DIR` for this task to succeed. If `CMAKE_SETTINGS_DIR` is set, a
89+
# settings file will be created in that directory, containing a `{{.NAME}}_ROOT` CMake variable
90+
# that points to `INSTALL_PREFIX`.
91+
#
92+
# NOTE: We purposely omit `sources` and `generates` as we defer to `cmake` to decide whether it
93+
# should perform any actions.
9094
#
9195
# @param {string} BUILD_DIR Directory containing the completed build to use.
9296
# @param {string} INSTALL_PREFIX Path prefix of where the project should be installed.
97+
# @param {string} NAME CMake project name (used in directory names and the CMake settings file).
98+
# @param {string} [CMAKE_SETTINGS_DIR] If set, the directory where the project's CMake settings
99+
# file should be stored.
93100
# @param {string[]} [EXTRA_ARGS] Any additional arguments to pass to the install command.
94101
install:
95102
internal: true
96103
label: "{{.TASK}}:{{.BUILD_DIR}}-{{.INSTALL_PREFIX}}-{{.EXTRA_ARGS}}"
97104
vars:
105+
CMAKE_SETTINGS_DIR: >-
106+
{{default "" .CMAKE_SETTINGS_DIR}}
98107
EXTRA_ARGS:
99108
ref: "default (list) .EXTRA_ARGS"
100109
requires:
101-
vars: ["BUILD_DIR", "INSTALL_PREFIX"]
110+
vars: ["BUILD_DIR", "INSTALL_PREFIX", "NAME"]
102111
cmds:
103112
- >-
104113
cmake
@@ -107,6 +116,14 @@ tasks:
107116
{{- range .EXTRA_ARGS}}
108117
"{{.}}"
109118
{{- end}}
119+
- >-
120+
{{- if .CMAKE_SETTINGS_DIR}}
121+
echo "set({{.NAME}}_ROOT
122+
\"{{.INSTALL_PREFIX}}\"
123+
CACHE PATH
124+
\"Package root for {{.NAME}}.\"
125+
)" >> "{{.CMAKE_SETTINGS_DIR}}/{{.NAME}}.cmake"
126+
{{- end}}
110127
111128
# Downloads a CMake project tar file from `URL` and then generates, builds, and installs the
112129
# project. We purposely omit `sources` and `generates` as we defer to `cmake` to decide whether it
@@ -135,6 +152,8 @@ tasks:
135152
# @param {string[]} [TARGETS] A list of specific targets to build instead of the default target.
136153
#
137154
# CMake install parameters
155+
# @param {string} [CMAKE_SETTINGS_DIR={{.WORK_DIR}}/cmake-settings] The directory where the
156+
# project's CMake settings file should be stored.
138157
# @param {string[]} [INSTALL_ARGS] Any additional arguments to pass to the CMake install command.
139158
# @param {string} [INSTALL_PREFIX={{.WORK_DIR}}/{{.NAME}}-install] Path prefix of where the
140159
# project should be installed.
@@ -163,6 +182,8 @@ tasks:
163182
ref: "default (list) .TARGETS"
164183

165184
# CMake install parameters
185+
CMAKE_SETTINGS_DIR: >-
186+
{{default (printf "%s/cmake-settings" .WORK_DIR) .CMAKE_SETTINGS_DIR}}
166187
INSTALL_ARGS:
167188
ref: "default (list) .INSTALL_ARGS"
168189
INSTALL_PREFIX: >-
@@ -193,6 +214,46 @@ tasks:
193214
- task: "install"
194215
vars:
195216
BUILD_DIR: "{{.BUILD_DIR}}"
217+
CMAKE_SETTINGS_DIR: "{{.CMAKE_SETTINGS_DIR}}"
196218
EXTRA_ARGS:
197219
ref: ".INSTALL_ARGS"
198220
INSTALL_PREFIX: "{{.INSTALL_PREFIX}}"
221+
NAME: "{{.NAME}}"
222+
223+
# Sets up all CMake dependencies for a project by:
224+
#
225+
# 1. creating a directory to contain all CMake settings files (CMAKE_SETTINGS_DIR).
226+
# 2. installing all dependencies by running `DEP_TASK`.
227+
# 3. combining each dependency's settings file into a single settings file (CMAKE_SETTINGS_FILE)
228+
# for use inside a CMake project.
229+
#
230+
# @param {string} CMAKE_SETTINGS_DIR The directory where CMake settings files should be stored.
231+
# @param {string} DEP_TASK The task to run that will install all dependencies. NOTE:
232+
# - The task name must be qualified from the root of the project.
233+
# - The task must not require any arguments (to use a task with arguments create a new task that
234+
# calls the original with any arguments set).
235+
# - Dependencies must write their settings file to CMAKE_SETTINGS_DIR in order to have them
236+
# included in CMAKE_SETTINGS_FILE.
237+
# @param {string} [CMAKE_SETTINGS_FILE={{.CMAKE_SETTINGS_DIR}}/all.cmake] The file in which to
238+
# combine each dependency's settings file.
239+
install-deps-and-generate-settings:
240+
internal: true
241+
label: "{{.TASK}}:{{.CMAKE_SETTINGS_DIR}}-{{.DEP_TASK}}"
242+
vars:
243+
CMAKE_SETTINGS_FILE: >-
244+
{{default (printf "%s/all.cmake" .CMAKE_SETTINGS_DIR) .CMAKE_SETTINGS_FILE}}
245+
requires:
246+
vars: ["CMAKE_SETTINGS_DIR", "DEP_TASK"]
247+
cmds:
248+
- "rm -rf {{.CMAKE_SETTINGS_DIR}}"
249+
- "mkdir -p {{.CMAKE_SETTINGS_DIR}}"
250+
251+
# NOTE: We prefix DEP_TASK with `::` assuming that this taskfile is included through the
252+
# `utils` taskfile, and that in turn is included in the user's taskfile.
253+
- task: "::{{.DEP_TASK}}"
254+
- |-
255+
for file in {{.CMAKE_SETTINGS_DIR}}/*.cmake; do
256+
if [[ "$file" != "{{.CMAKE_SETTINGS_FILE}}" ]]; then
257+
echo "include(\"$file\")" >> "{{.CMAKE_SETTINGS_FILE}}";
258+
fi
259+
done

0 commit comments

Comments
 (0)