From 1fea2dfd12d3bdf894c51ec0ce305a8ef55ab746 Mon Sep 17 00:00:00 2001 From: davidlion Date: Thu, 14 Nov 2024 11:40:12 -0500 Subject: [PATCH] Add cmake and remote utils; change checksum data path to an array of paths. --- taskfiles/utils.yml | 185 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 173 insertions(+), 12 deletions(-) diff --git a/taskfiles/utils.yml b/taskfiles/utils.yml index 13bdded..828912e 100644 --- a/taskfiles/utils.yml +++ b/taskfiles/utils.yml @@ -5,10 +5,10 @@ tasks: # CHECKSUM UTILS # === - # @param {string} DATA_DIR The directory to compute the checksum for. + # @param {[]string} [DATA_PATHS] List of paths to compute the checksum for. # @param {string} OUTPUT_FILE - # @param {[]string} [EXCLUDE_PATHS] A list of paths, relative to `DATA_DIR`, to exclude from the - # checksum. + # @param {[]string} [EXCLUDE_PATHS] List of paths, relative to any `DATA_PATHS`, to exclude from + # the checksum. compute-checksum: desc: "Tries to compute a checksum for the given directory and output it to a file." internal: true @@ -18,7 +18,6 @@ tasks: cmds: - >- tar cf - - --directory "{{.DATA_DIR}}" --group 0 --mtime "UTC 1970-01-01" --numeric-owner @@ -27,15 +26,18 @@ tasks: {{- range .EXCLUDE_PATHS}} --exclude="{{.}}" {{- end}} - . 2> /dev/null + {{- range .DATA_PATHS}} + "{{.}}" + {{- end}} + 2> /dev/null | md5sum > {{.OUTPUT_FILE}} # Ignore errors so that dependent tasks don't fail ignore_error: true - # @param {string} DATA_DIR The directory to validate the checksum for. + # @param {[]string} [DATA_PATHS] List of paths to validate the checksum for. # @param {string} OUTPUT_FILE - # @param {[]string} [EXCLUDE_PATHS] A list of paths, relative to `DATA_DIR`, to exclude from the - # checksum. + # @param {[]string} [EXCLUDE_PATHS] List of paths, relative to any `DATA_PATHS`, to exclude from + # the checksum. validate-checksum: desc: "Validates the checksum of the given directory matches the checksum in the given file, or deletes the checksum file otherwise." @@ -48,16 +50,19 @@ tasks: cmds: - task: "compute-checksum" vars: - DATA_DIR: "{{.DATA_DIR}}" + DATA_PATHS: + ref: ".DATA_PATHS" EXCLUDE_PATHS: - ref: "default (list) .EXCLUDE_PATHS" + ref: ".EXCLUDE_PATHS" OUTPUT_FILE: "{{.TMP_CHECKSUM_FILE}}" - defer: "rm -f '{{.TMP_CHECKSUM_FILE}}'" # Check that the directory exists and the checksum matches; otherwise delete the checksum file - >- ( - test -d "{{.DATA_DIR}}" - && diff -q '{{.TMP_CHECKSUM_FILE}}' '{{.CHECKSUM_FILE}}' 2> /dev/null + {{- range .DATA_PATHS}} + test -e "{{.}}" && + {{- end}} + diff -q '{{.TMP_CHECKSUM_FILE}}' '{{.CHECKSUM_FILE}}' 2> /dev/null ) || rm -f '{{.CHECKSUM_FILE}}' # === @@ -141,3 +146,159 @@ tasks: \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.hpp" \) \ -print0 | \ xargs -0 clang-tidy {{.FLAGS}} + + # === + # CMAKE UTILS + # === + + # Runs cmake configure and build steps for the given source and build directories. + # + # @param {string} BUILD_DIR Cmake build directory to create. + # @param {string} SOURCE_DIR Project source directory containing the CMakeLists.txt file. + # @param {string}[optional] CHECKSUM_FILE Path to store the checksum of built files. + # @param {string}[optional] CMAKE_ARGS Any additional arguments to pass to cmake configure. + cmake-build: + label: "cmake-build: {{.SOURCE_DIR}} {{.BUILD_DIR}}" + internal: true + vars: + CHECKSUM_FILE: '{{default (printf "%s.md5" .BUILD_DIR) .CHECKSUM_FILE}}' + CMAKE_ARGS: "{{default nil .CMAKE_ARGS}}" + requires: + vars: ["BUILD_DIR", "SOURCE_DIR"] + sources: + - "{{.SOURCE_DIR}}/**/*" + - exclude: "{{.SOURCE_DIR}}/.cache/**/*" + - exclude: "{{.SOURCE_DIR}}/compile_commands.json" + generates: ["{{.CHECKSUM_FILE}}"] + deps: + - task: "validate-checksum" + vars: + CHECKSUM_FILE: "{{.CHECKSUM_FILE}}" + DATA_PATHS: ["{{.BUILD_DIR}}"] + EXCLUDE_PATHS: ["install_manifest.txt"] + cmds: + - >- + cmake + -S "{{.SOURCE_DIR}}" + -B "{{.BUILD_DIR}}" + {{.CMAKE_ARGS}} + - >- + cmake + --build "{{.BUILD_DIR}}" + --parallel + # This command must be last + - task: "compute-checksum" + vars: + DATA_PATHS: ["{{.BUILD_DIR}}"] + OUTPUT_FILE: "{{.CHECKSUM_FILE}}" + EXCLUDE_PATHS: ["install_manifest.txt"] + + # Runs cmake install step for the given build directory. + # + # @param {string} BUILD_DIR Cmake build directory. + # @param {string} INSTALL_PREFIX Path prefix for installing the project. + # @param {string}[optional] CHECKSUM_FILE Path to store the checksum of installed files. + # @param {[]string}[optional] DATA_PATHS Paths to compute the the checksum for. Overrides using + # the install prefix path. + cmake-install: + label: "cmake-install: {{.BUILD_DIR}} {{.INSTALL_PREFIX}}" + internal: true + vars: + CHECKSUM_FILE: '{{default (printf "%s.md5" .INSTALL_PREFIX) .CHECKSUM_FILE}}' + # Convert the install prefix to a single element array for the default case where DATA_PATHS + # is not manually set. + _INSTALL_PREFIX_ARRAY: ["{{.INSTALL_PREFIX}}"] + DATA_PATHS: + ref: "default ._INSTALL_PREFIX_ARRAY .DATA_PATHS" + requires: + vars: ["BUILD_DIR", "INSTALL_PREFIX"] + sources: + - "{{.BUILD_DIR}}/**/*" + - exclude: "{{.BUILD_DIR}}/install_manifest.txt" + generates: ["{{.CHECKSUM_FILE}}"] + deps: + - task: "validate-checksum" + vars: + CHECKSUM_FILE: "{{.CHECKSUM_FILE}}" + DATA_PATHS: + ref: ".DATA_PATHS" + cmds: + - >- + cmake + --install "{{.BUILD_DIR}}" + --prefix "{{.INSTALL_PREFIX}}" + # This command must be last + - task: "compute-checksum" + vars: + DATA_PATHS: + ref: ".DATA_PATHS" + OUTPUT_FILE: "{{.CHECKSUM_FILE}}" + + # === + # REMOTE UTILS + # === + + # Runs curl to download the provided URL. + # + # @param {string} URL + # @param {string} URL_SHA256 Content hash to verify downloaded file against. + # @param {string}[optional] OUTPUT_FILE File path to store the download file. + curl: + label: "curl: {{.OUTPUT_FILE}}" + internal: true + vars: + OUTPUT_FILE: "{{default (base .URL) .OUTPUT_FILE}}" + requires: + vars: ["URL", "URL_SHA256"] + generates: + - "{{.OUTPUT_FILE}}" + status: + - >- + diff + <(echo "{{.URL_SHA256}}") + <(openssl dgst -sha256 "{{.OUTPUT_FILE}}" + | awk '{print $2}') + cmds: + - |- + mkdir -p "{{dir .OUTPUT_FILE}}" + curl -L "{{.URL}}" -o "{{.OUTPUT_FILE}}" + + # Runs curl to download the provided URL and tar to extract its contents. + # + # @param {string} OUTPUT_DIR Path to extract downloaded tar file contents to. + # @param {string} URL + # @param {string} URL_SHA256 Content hash to verify downloaded tar file against. + # @param {string}[optional] CHECKSUM_FILE File path to store the checksum of downloaded tar file. + # @param {int}[optional] STRIP Number of leading components to strip from file names for tar. + # @param {string}[optional] TAR_FILE File path to store the downloaded tar file. + fetch-src: + label: "fetch-src: {{.OUTPUT_DIR}}" + internal: true + vars: + CHECKSUM_FILE: '{{default (printf "%s.md5" .OUTPUT_DIR) .CHECKSUM_FILE}}' + STRIP: "{{default 1 .STRIP}}" + TAR_FILE: '{{default (printf "%s.tar.gz" .OUTPUT_DIR) .TAR_FILE}}' + requires: + vars: ["OUTPUT_DIR", "URL", "URL_SHA256"] + sources: ["{{.TASKFILE}}"] + generates: ["{{.CHECKSUM_FILE}}", "{{.TAR_FILE}}"] + deps: + - task: "curl" + vars: + URL: "{{.URL}}" + URL_SHA256: "{{.URL_SHA256}}" + OUTPUT_FILE: "{{.TAR_FILE}}" + - task: "validate-checksum" + vars: + CHECKSUM_FILE: "{{.CHECKSUM_FILE}}" + DATA_PATHS: ["{{.OUTPUT_DIR}}"] + cmds: + - |- + rm -rf "{{.OUTPUT_DIR}}" + mkdir -p "{{.OUTPUT_DIR}}" + tar -x --strip-components="{{.STRIP}}" -C "{{.OUTPUT_DIR}}" -f "{{.TAR_FILE}}" + # This command must be last + - task: "compute-checksum" + vars: + DATA_PATHS: ["{{.OUTPUT_DIR}}"] + OUTPUT_FILE: "{{.CHECKSUM_FILE}}"