From 330da09665c805c70ee41aba7eacbc5e484996c3 Mon Sep 17 00:00:00 2001 From: Sean Parent Date: Tue, 11 Jun 2024 12:15:54 -0700 Subject: [PATCH] Fixing docker build (#87) - Speedup and simplify docker build - Fix compiler errors when building with clang-15 - Update readme on building with docker - Minor reformatting (using existing `.clang-format` file) --- Dockerfile | 15 +++++++-------- README.md | 30 ++++++++++++++++++++++++++++-- sources/autodetect.cpp | 38 ++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7630e5c..eb55d52 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,17 +24,12 @@ RUN apt-get -y install build-essential # directory with # `hyde ./test.hpp -- -x c++ -print-resource-dir` +# FROM base AS full + ENV LLVM_VERSION=15 RUN apt-get -y install clang-${LLVM_VERSION} - -# The above doesn't setup libc++ header paths correctly. Currently LLVM-15 -# doesn't install with llvm.sh in docker. So we install LLVM-16 just for -# the libc++ config! - -RUN wget https://apt.llvm.org/llvm.sh -RUN chmod +x llvm.sh -RUN ./llvm.sh 16 all +RUN apt-get -y install libc++-${LLVM_VERSION}-dev # set clang ${LLVM_VERSION} to be the version of clang we use when clang/clang++ is invoked RUN update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-${LLVM_VERSION} 100 @@ -64,3 +59,7 @@ RUN cp ./build/hyde /usr/bin # RUN apt-get -y install clang-15 CMD ["./generate_test_files.sh"] + +# Experimenting with publishing the container and linking it to the hyde repo: + +LABEL org.opencontainers.image.source=https://github.com/adobe/hyde diff --git a/README.md b/README.md index c6a24e8..379401c 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,21 @@ LLVM/Clang are declared as a dependency in the project's `CMakeLists.txt` file, and will be downloaded and made available to the project automatically. -# Using Docker +# How to run from Docker -You may need to increase your docker resources to build the image. +```sh +docker pull ghcr.io/adobe/hyde:latest + +docker run --platform linux/x86_64 --mount type=bind,source="$(pwd)",target=/mnt/host \ + --tty --interactive \ + ghcr.io/adobe/hyde:latest bash +``` + +You can then run the examples as below, except don't prefix `hyde` with `./`. + +# Building the Docker image + +You may need to increase your docker resources to build the image. (2.0.1 successfully built with 16GB RAM and 4GB swap) ```sh docker build --tag hyde . @@ -51,6 +63,20 @@ docker run --platform linux/x86_64 --mount type=bind,source="$(pwd)",target=/mnt hyde bash ``` +# Publishing the docker image (requires write access to the `adobe` GitHub organization) + +Instructions for publishing a GitHub package can be found [here](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry). +Instructions for associating the with the `adobe/hyde` repository can be found [here](https://docs.github.com/en/packages/learn-github-packages/connecting-a-repository-to-a-package#connecting-a-repository-to-a-container-image-using-the-command-line). + +```sh +VERSION=2.0.1 +docker tag hyde ghcr.io/adobe/hyde:$VERSION +docker tag hyde ghcr.io/adobe/hyde:latest +docker push ghcr.io/adobe/hyde:$VERSION +docker push ghcr.io/adobe/hyde:latest +``` + + # Parameters and Flags There are several modes under which the tool can run: diff --git a/sources/autodetect.cpp b/sources/autodetect.cpp index aa82604..406b2aa 100644 --- a/sources/autodetect.cpp +++ b/sources/autodetect.cpp @@ -13,9 +13,16 @@ written permission of Adobe. #include "autodetect.hpp" // stdc++ +#include #include -#include +#include #include +#include +#include +#include +#include +#include +#include /**************************************************************************************************/ @@ -28,9 +35,11 @@ std::vector split(const char* p, std::size_t n) { std::vector result; while (p != end) { - while (p != end && *p == '\n') ++p; // eat newlines + while (p != end && *p == '\n') + ++p; // eat newlines auto begin = p; - while (p != end && *p != '\n') ++p; // eat non-newlines + while (p != end && *p != '\n') + ++p; // eat non-newlines result.emplace_back(begin, p); } @@ -78,22 +87,26 @@ std::string trim_back(std::string s) { /**************************************************************************************************/ -std::string chomp(std::string src) { - return trim_back(trim_front(std::move(src))); -} +std::string chomp(std::string src) { return trim_back(trim_front(std::move(src))); } /**************************************************************************************************/ std::string exec(const char* cmd) { - std::array buffer; - std::string result; - std::unique_ptr pipe(popen(cmd, "r"), pclose); + struct pclose_t { + void operator()(std::FILE* p) const { (void)pclose(p); } + }; + std::unique_ptr pipe{popen(cmd, "r")}; + if (!pipe) { throw std::runtime_error("popen() failed!"); } + + std::array buffer; + std::string result; while (fgets(buffer.data(), buffer.size(), pipe.get())) { result += buffer.data(); } + return chomp(std::move(result)); } @@ -105,7 +118,8 @@ std::vector autodetect_include_paths() { auto temp_dir = std::filesystem::temp_directory_path(); auto temp_out = (temp_dir / ("hyde_" + v + ".tmp")).string(); auto temp_a_out = (temp_dir / ("deleteme_" + v)).string(); - auto command = "echo \"int main() { }\" | clang++ -x c++ -v -o " + temp_a_out + " - 2> " + temp_out; + auto command = + "echo \"int main() { }\" | clang++ -x c++ -v -o " + temp_a_out + " - 2> " + temp_out; auto command_result = std::system(command.c_str()); (void)command_result; // TODO: handle me @@ -119,14 +133,14 @@ std::vector autodetect_include_paths() { if (paths_begin != end(lines) && paths_end != end(lines)) { lines.erase(paths_end, end(lines)); - lines.erase(begin(lines), next(paths_begin)); + lines.erase(begin(lines), std::next(paths_begin)); // lines.erase(std::remove_if(begin(lines), end(lines), [](auto& s){ // return s.find(".sdk/") != std::string::npos; // }), end(lines)); // Some of the paths contain cruft at the end. Filter those out, too. - std::transform(begin(lines), end(lines), std::back_inserter(result), [](auto s){ + std::transform(begin(lines), end(lines), std::back_inserter(result), [](auto s) { static const std::string needle_k{" (framework directory)"}; auto needle_pos = s.find(needle_k); if (needle_pos != std::string::npos) {