From 1acb8dbcdfa06f8078069c8b873a651da4f7d1f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Linjam=C3=A4ki?= Date: Wed, 5 Oct 2022 13:29:29 +0300 Subject: [PATCH] Try use if is not provided --- CMakeLists.txt | 10 ++++++++++ Config.hh.in | 4 ++++ src/Filesystem.hh | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/Utils.cc | 2 -- src/Utils.hh | 11 +++++------ src/spirv_hiprtc.cc | 3 --- 6 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/Filesystem.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index caa86948c..cb33ac357 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,16 @@ set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) set(PTHREAD_LIBRARY Threads::Threads) +include(CheckIncludeFileCXX) +check_include_file_cxx("filesystem" HAS_FILESYSTEM) +if(NOT HAS_FILESYSTEM) + check_include_file_cxx("experimental/filesystem" HAS_EXPERIMENTAL_FILESYSTEM) +endif() + +if(NOT HAS_FILESYSTEM AND NOT HAS_EXPERIMENTAL_FILESYSTEM) + message(FATAL_ERROR " was not found.") +endif() + # CHIP-SPV CMAKE DEPENDENCIES ############################################################################### diff --git a/Config.hh.in b/Config.hh.in index 2e803aa21..90bd25564 100644 --- a/Config.hh.in +++ b/Config.hh.in @@ -31,4 +31,8 @@ #cmakedefine CHIP_CLANG_PATH "@CHIP_CLANG_PATH@" +#cmakedefine HAS_FILESYSTEM @HAS_FILESYSTEM@ + +#cmakedefine HAS_EXPERIMENTAL_FILESYSTEM @HAS_EXPERIMENTAL_FILESYSTEM@ + #endif diff --git a/src/Filesystem.hh b/src/Filesystem.hh new file mode 100644 index 000000000..14c8fc548 --- /dev/null +++ b/src/Filesystem.hh @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Henry Linjamäki / Parmance for Argonne National Laboratory + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +// Provides a namespace alias (fs) over std::filesystem-like module. + +#ifndef SRC_FILESYSTEM_HH +#define SRC_FILESYSTEM_HH + +#include "Config.hh" + +#if defined(HAS_FILESYSTEM) && HAS_FILESYSTEM == 1 + +#include +namespace fs = std::filesystem; + +#elif defined(HAS_EXPERIMENTAL_FILESYSTEM) && HAS_EXPERIMENTAL_FILESYSTEM == 1 + +#include +namespace fs = std::experimental::filesystem; + +#else +#error filesystem is not available! +#endif + +#endif diff --git a/src/Utils.cc b/src/Utils.cc index d670b20c0..3a3b236f7 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -28,8 +28,6 @@ #include #include -namespace fs = std::filesystem; - template static T copyAs(const void *BaseAddr, size_t ByteOffset = 0) { T Res; diff --git a/src/Utils.hh b/src/Utils.hh index 13d9bcd78..e164c8c46 100644 --- a/src/Utils.hh +++ b/src/Utils.hh @@ -25,10 +25,9 @@ #define SRC_UTILS_HH #include "common.hh" - +#include "Filesystem.hh" #include "hip/hip_fatbin.h" -#include #include /// Clamps 'Val' to [0, INT_MAX] range. @@ -53,17 +52,17 @@ static inline size_t roundUp(size_t Val, size_t Rounding) { std::string getRandomString(size_t N); /// Return a unique directory for temporary use. -std::optional createTemporaryDirectory(); +std::optional createTemporaryDirectory(); /// Write 'Data' into 'Path' file. If the file exists, its content is /// overwriten. Return false on errors. -bool writeToFile(const std::filesystem::path Path, const std::string &Data); +bool writeToFile(const fs::path Path, const std::string &Data); /// Reads contents of file from 'Path' into a std::string. -std::optional readFromFile(const std::filesystem::path Path); +std::optional readFromFile(const fs::path Path); /// Locate hipcc tool. Return an absolute path to it if found. -std::optional getHIPCCPath(); +std::optional getHIPCCPath(); /// Returns a span (string_view) over SPIR-V module in the given clang /// offload bundle. Returns empty span if an error was encountered diff --git a/src/spirv_hiprtc.cc b/src/spirv_hiprtc.cc index 1bf7fe803..fdf323038 100644 --- a/src/spirv_hiprtc.cc +++ b/src/spirv_hiprtc.cc @@ -27,13 +27,10 @@ THE SOFTWARE. #include "CHIPBackend.hh" #include "Utils.hh" -#include #include #include #include -namespace fs = std::filesystem; - /// Checks the name is valid string for #include (both the "" and <> /// forms) and is sensible name for shells as is (doesn't need escaping). static bool checkIncludeName(std::string_view Name) {