diff --git a/.github/actions/install-cmake-build-dependencies/action.yaml b/.github/actions/install-cmake-build-dependencies/action.yaml new file mode 100644 index 0000000000..4d75b2538d --- /dev/null +++ b/.github/actions/install-cmake-build-dependencies/action.yaml @@ -0,0 +1,27 @@ +--- +name: Install CMake build dependencies +description: Install CMake build dependencies +runs: + using: composite + steps: + - uses: ./.github/actions/install-build-dependencies + + - name: Install CMake dependencies + run: | + if [ "$(uname)" = "Darwin" ]; then + echo "CMake build for Darwin is unimplemented." + exit 1 + else + # Compiler + sudo apt-get install clang++-9 lld-9 tar bzip2 ninja-build + sudo apt-get install tar bzip2 ninja-build + # CMake + wget https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5-linux-x86_64.sh -O /tmp/cmake.sh + sudo bash /tmp/cmake.sh --prefix=/usr/local --exclude-subdir --skip-license + rm /tmp/cmake.sh + # protobuf + sudo apt-get install autoconf libtool make + # Testing + sudo apt-get install coreutils + fi + shell: bash diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 872f438c67..f2de69995d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -52,6 +52,46 @@ jobs: if-no-files-found: error retention-days: 7 + build-linux-cmake: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install build dependencies + uses: ./.github/actions/install-cmake-build-dependencies + + - name: CMake Build + run: | + cmake \ + -GNinja \ + -DCMAKE_C_COMPILER=clang-9 \ + -DCMAKE_CXX_COMPILER=clang++-9 \ + -DCMAKE_EXE_LINKER_FLAGS_INIT="-fuse-ld=lld" \ + -DCMAKE_MODULE_LINKER_FLAGS_INIT="-fuse-ld=lld" \ + -DCMAKE_SHARED_LINKER_FLAGS_INIT="-fuse-ld=lld" \ + -DPython3_FIND_VIRTUALENV=FIRST \ + -DCOMPILER_GYM_BUILD_TESTS=ON \ + -S . \ + -B ~/cmake_build + cmake --build ~/cmake_build + shell: bash + + - name: Install runtime dependencies + uses: ./.github/actions/install-runtime-dependencies + + - name: Install test dependencies + run: python -m pip install -r tests/requirements.txt + + - name: Run the test suite + run: | + cd ~/cmake_build + ctest --parallel $(nproc) --tests-regex tests/ --label-exclude manual + shell: bash + build-macos: runs-on: macos-latest steps: @@ -125,6 +165,7 @@ jobs: - name: Upload coverage report to Codecov uses: codecov/codecov-action@v2 + test-macos: needs: build-macos runs-on: macos-latest diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000..4ed57c2bc5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,68 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cmake_minimum_required(VERSION 3.20) + +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) + message(FATAL_ERROR "In-source builds are unsupported. Please, build out of the source tree.") +endif() + +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(DARWIN TRUE) +endif() + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +project(compiler_gym ASM C CXX) + +set(CMAKE_C_STANDARD 11 CACHE STRING "C standard to be used.") +set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to be used.") + +set_property(GLOBAL PROPERTY USE_FOLDERS ON) + +list(APPEND CMAKE_MODULE_PATH + ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/ +) + +set(COMPILER_GYM_BUILD_TESTS OFF CACHE BOOL "Enable Compiler Gym tests.") + +include(cg_macros) +include(cg_copts) +include(cg_genrule) +include(cg_cc_binary) +include(cg_cc_library) +include(cg_cc_test) +include(cg_py_binary) +include(cg_py_library) +include(cg_py_test) +include(cg_python) +include(cg_add_all_subdirs) +include(cg_filegroup) +include(grpc) +include(protobuf) + +set(COMPILER_GYM_PYTHONPATH "$ENV{PYTHONPATH}" CACHE STRING "PYTHONPATH environment variable during build step.") +if (COMPILER_GYM_PYTHONPATH) + string(PREPEND COMPILER_GYM_PYTHONPATH ":") +endif() +string(PREPEND COMPILER_GYM_PYTHONPATH "${CMAKE_BINARY_DIR}") +include(set_command_pythonpath) + +set(DEFAULT_CMAKE_BUILD_TYPE "Release") +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}") + set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE) +endif() + +set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) + +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +include(external/external.cmake) +add_subdirectory(compiler_gym) +if(COMPILER_GYM_BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/INSTALL.md b/INSTALL.md index e05451afe9..a1f6be4757 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -7,12 +7,12 @@ Install the latest CompilerGym release using: CompilerGym requires Python >= 3.6. The binary works on macOS and Linux (on Ubuntu 18.04, Fedora 28, Debian 10 or newer equivalents). -## Building from Source +# Building from Source If you prefer, you may build from source. This requires a modern C++ toolchain and bazel. -### macOS +## macOS On macOS the required dependencies can be installed using [homebrew](https://docs.brew.sh/Installation): @@ -26,12 +26,13 @@ export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig" Now proceed to [All platforms](#all-platforms) below. -### Linux +## Linux On debian-based linux systems, install the required toolchain using: ```sh -sudo apt install clang-9 clang-format golang libjpeg-dev libtinfo5 m4 make patch zlib1g-dev +sudo apt install clang-9 clang++-9 clang-format golang libjpeg-dev \ + libtinfo5 m4 make patch zlib1g-dev tar bzip2 wget mkdir -pv ~/.local/bin wget https://github.com/bazelbuild/bazelisk/releases/download/v1.7.5/bazelisk-linux-amd64 -O ~/.local/bin/bazel wget https://github.com/hadolint/hadolint/releases/download/v1.19.0/hadolint-Linux-x86_64 -O ~/.local/bin/hadolint @@ -44,7 +45,7 @@ export CXX=clang++ ``` -### All platforms +## All platforms We recommend using [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/) @@ -70,6 +71,8 @@ your preferred branch and install the python development dependencies using: The `make init` target only needs to be run on initial setup and after pulling remote changes to the CompilerGym repository. +## Building from source with Bazel + Run the test suite to confirm that everything is working: make test @@ -87,3 +90,70 @@ environment using: conda deactivate conda env remove -n compiler_gym + +## Building from source with CMake + +### Dependency instructions for Ubuntu + +```bash +sudo apt-get install lld-9 \ + autoconf libtool ninja-build ccache git \ +``` + +Requires CMake (>=3.20). + +```bash +wget https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5-linux-x86_64.sh -O cmake.sh +bash cmake.sh --prefix=$HOME/.local --exclude-subdir --skip-license +rm cmake.sh +``` + +### Dependency Arguments +By default most dependencies are built together with Compiler Gym. To search for a dependency instead use: + +``` +-DCOMPILER_GYM__PROVIDER=external +``` + +* `COMPILER_GYM_BOOST_PROVIDER` +* `COMPILER_GYM_GFLAGS_PROVIDER` +* `COMPILER_GYM_GLOG_PROVIDER` +* `COMPILER_GYM_GRPC_PROVIDER` +* `COMPILER_GYM_GTEST_PROVIDER` +* `COMPILER_GYM_NLOHMANN_JSON_PROVIDER` +* `COMPILER_GYM_PROTOBUF_PROVIDER` + +```bash +cmake -GNinja \ + -DCMAKE_C_COMPILER=clang-9 \ + -DCMAKE_CXX_COMPILER=clang++-9 \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ # For faster rebuilds, can be removed + -DCMAKE_EXE_LINKER_FLAGS_INIT="-fuse-ld=lld" -DCMAKE_MODULE_LINKER_FLAGS_INIT="-fuse-ld=lld" -DCMAKE_SHARED_LINKER_FLAGS_INIT="-fuse-ld=lld" \ # For faster builds, can be removed + -DPython3_FIND_VIRTUALENV=FIRST \ + -S "" \ + -B "" + +cmake --build "" +``` +Additional optional configuration arguments: + +* Enables testing. + + ```bash + -DCOMPILER_GYM_BUILD_TESTS=ON + ``` + +* For faster linking. + + ```bash + -DCMAKE_EXE_LINKER_FLAGS_INIT="-fuse-ld=lld-9" + -DCMAKE_MODULE_LINKER_FLAGS_INIT="-fuse-ld=lld-9" + -DCMAKE_SHARED_LINKER_FLAGS_INIT="-fuse-ld=lld-9" + ``` + +* For faster rebuilds. + + ```bash + -DCMAKE_C_COMPILER_LAUNCHER=ccache + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache + ``` diff --git a/WORKSPACE b/WORKSPACE index a8caf96c06..95b2076862 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -285,9 +285,9 @@ http_file( http_archive( name = "cpuinfo", build_file_content = all_content, - sha256 = "18a99130ced1eaacab2ba8f75a1435f9955aab54fa0436b60468f020876ee902", - strip_prefix = "cpuinfo-63b254577ed77a8004a9be6ac707f3dccc4e1fd9", - urls = ["https://github.com/pytorch/cpuinfo/archive/63b254577ed77a8004a9be6ac707f3dccc4e1fd9.tar.gz"], + sha256 = "b9874dbb2f9436c9d0d7f42aaf3f94f1af3da37bc0b250268760ada2507ca543", + strip_prefix = "cpuinfo-2e79955ecaec85da13ac8f1245a8b2afa10d31c2", + urls = ["https://github.com/pytorch/cpuinfo/archive/2e79955ecaec85da13ac8f1245a8b2afa10d31c2.tar.gz"], ) # === Csmith === diff --git a/build_tools/cmake/FindBazel.cmake b/build_tools/cmake/FindBazel.cmake new file mode 100644 index 0000000000..4860d94151 --- /dev/null +++ b/build_tools/cmake/FindBazel.cmake @@ -0,0 +1,53 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``Bazel_FOUND`` + true if Bazel is available. +``Bazel_VERSION`` + the version of Bazel. +``Bazel_EXECUTABLE`` + Path to the Bazel executable. + +#]=======================================================================] + +find_program(Bazel_EXECUTABLE bazel) + +execute_process(COMMAND "${Bazel_EXECUTABLE}" version + RESULT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE + OUTPUT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE + ERROR_QUIET +) + +set(Bazel_VERSION) + +if(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE EQUAL 0) + string(REGEX MATCH "Build label: ([0-9a-zA-Z.]+)" + _BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE + "${_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE}" + ) + + if(CMAKE_MATCH_1) + set(Bazel_VERSION "${CMAKE_MATCH_1}") + endif() + + unset(_BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE) +endif() + +unset(_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE) +unset(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(Bazel + FOUND_VAR Bazel_FOUND + REQUIRED_VARS Bazel_EXECUTABLE + VERSION_VAR Bazel_VERSION +) diff --git a/build_tools/cmake/FindClog.cmake b/build_tools/cmake/FindClog.cmake new file mode 100644 index 0000000000..43c1ed1770 --- /dev/null +++ b/build_tools/cmake/FindClog.cmake @@ -0,0 +1,42 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Find Clog headers and libraries. + +Imported Targets +^^^^^^^^^^^^^^^^ + +``Clog::libclog`` + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``Clog_FOUND`` + true if Clog is available. + + +#]=======================================================================] + +include(FindPackageHandleStandardArgs) + +find_path(Clog_INCLUDE_DIRS clog.h + PATH_SUFFIXES include) + +find_library(Clog_LIBRARIES clog PATH_SUFFIXES lib) +if(Clog_INCLUDE_DIRS AND Clog_LIBRARIES) + add_library(Clog::libclog UNKNOWN IMPORTED) + set_target_properties(Clog::libclog PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Clog_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${Clog_LIBRARIES}") +endif() +find_package_handle_standard_args( + Clog + REQUIRED_VARS + Clog_INCLUDE_DIRS + Clog_LIBRARIES) diff --git a/build_tools/cmake/FindCsmith.cmake b/build_tools/cmake/FindCsmith.cmake new file mode 100644 index 0000000000..114f74f892 --- /dev/null +++ b/build_tools/cmake/FindCsmith.cmake @@ -0,0 +1,75 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Find Csmith headers and library. + +Imported Targets +^^^^^^^^^^^^^^^^ + +``Csmith::libcsmith`` + The Csmith library, if found. +``Csmith::csmith`` + The Csmith executable. + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``Csmith_FOUND`` + true if Csmith is available. +``Csmith_VERSION`` + the version of Csmith. +``Csmith_ROOT_DIR`` +``Csmith_EXECUTABLE`` +``Csmith_LIBRARIES`` + the libraries to link against to use Csmith. +``Csmith_LIBRARY_DIRS`` + the directories of the Csmith libraries. +``Csmith_INCLUDE_DIRS`` + where to find the libinput headers. + + +#]=======================================================================] + +include(FindPackageHandleStandardArgs) + +find_program(Csmith_EXECUTABLE csmith) +if (Csmith_EXECUTABLE) + execute_process( + COMMAND "${Csmith_EXECUTABLE}" --version + OUTPUT_VARIABLE Csmith_VERSION) + string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" Csmith_VERSION "${Csmith_VERSION}") + + add_executable(Csmith::csmith IMPORTED GLOBAL) + set_target_properties(Csmith::csmith PROPERTIES IMPORTED_LOCATION "${Csmith_EXECUTABLE}") + + get_filename_component(Csmith_ROOT_DIR "${Csmith_EXECUTABLE}" DIRECTORY) + get_filename_component(Csmith_ROOT_DIR "${Csmith_ROOT_DIR}/.." ABSOLUTE) + set(Csmith_ROOT_DIR "${Csmith_ROOT_DIR}" CACHE string "Path to the root installation directory of Csmith.") +endif() +find_path(Csmith_INCLUDE_DIRS csmith.h PATH_SUFFIXES csmith csmith-2.3.0) +find_library(Csmith_LIBRARIES csmith) +if (Csmith_LIBRARIES) + get_filename_component(Csmith_LIBRARY_DIRS "${Csmith_LIBRARIES}" DIRECTORY) +endif() +if (Csmith_LIBRARIES AND Csmith_INCLUDE_DIRS) + add_library(Csmith::libcsmith UNKNOWN IMPORTED) + set_target_properties(Csmith::libcsmith PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Csmith_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${Csmith_LIBRARIES}") +endif() + +find_package_handle_standard_args(Csmith + REQUIRED_VARS + Csmith_ROOT_DIR + Csmith_EXECUTABLE + Csmith_INCLUDE_DIRS + Csmith_LIBRARIES + Csmith_LIBRARY_DIRS + VERSION_VAR Csmith_VERSION + HANDLE_VERSION_RANGE) diff --git a/build_tools/cmake/FindLabm8.cmake b/build_tools/cmake/FindLabm8.cmake new file mode 100644 index 0000000000..811028c2e1 --- /dev/null +++ b/build_tools/cmake/FindLabm8.cmake @@ -0,0 +1,146 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Find Labm8 headers and libraries. + +Imported Targets +^^^^^^^^^^^^^^^^ + +``Labm8::cpp::status`` +``Labm8::cpp::statusor`` +``Labm8::cpp::logging`` +``Labm8::cpp::string`` +``Labm8::cpp::stringpiece`` + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``Labm8_FOUND`` + true if Labm8 is available. + + +#]=======================================================================] + +include(FindPackageHandleStandardArgs) + +function(has_absl _RES_VAR) + if(TARGET absl::strings AND + TARGET absl::time) + set(${_RES_VAR} True PARENT_SCOPE) + else() + set(${_RES_VAR} False PARENT_SCOPE) + endif() +endfunction() + +function(has_fmt _RES_VAR) + if(TARGET fmt) + set(${_RES_VAR} True PARENT_SCOPE) + else() + set(${_RES_VAR} False PARENT_SCOPE) + endif() +endfunction() + +if(Labm8_FIND_REQUIRED) + set(_REQUIRED REQUIRED) +endif() + +has_absl(Labm8_HAS_absl) +if(NOT Labm8_HAS_absl) + find_package(absl ${_REQUIRED}) + has_absl(Labm8_HAS_absl) +endif() + +has_fmt(Labm8_HAS_fmt) +if(NOT Labm8_HAS_fmt) + find_package(fmt ${_REQUIRED}) + has_fmt(Labm8_HAS_fmt) +endif() + +find_path(Labm8_INCLUDE_DIRS + labm8/cpp/status.h + ) + +find_library(Labm8_cpp_string_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}string${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES labm8/cpp) +if(Labm8_INCLUDE_DIRS AND Labm8_cpp_string_LIBRARIES) + add_library(Labm8::cpp::string UNKNOWN IMPORTED) + set_target_properties(Labm8::cpp::string PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Labm8_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${Labm8_cpp_string_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES absl::strings) +endif() + +find_library(Labm8_cpp_stringpiece_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}stringpiece${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES labm8/cpp) +if(Labm8_INCLUDE_DIRS AND Labm8_cpp_stringpiece_LIBRARIES) + add_library(Labm8::cpp::stringpiece UNKNOWN IMPORTED) + set_target_properties(Labm8::cpp::stringpiece PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Labm8_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${Labm8_cpp_stringpiece_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES Labm8::cpp::string) +endif() + +find_library(Labm8_cpp_status_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}status${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES labm8/cpp) +if(Labm8_INCLUDE_DIRS AND Labm8_cpp_status_LIBRARIES) + add_library(Labm8::cpp::status UNKNOWN IMPORTED) + set(_LINK_LIBS + Labm8::cpp::string + Labm8::cpp::stringpiece + fmt) + set_target_properties(Labm8::cpp::status PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Labm8_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${Labm8_cpp_status_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES "${_LINK_LIBS}") +endif() + +find_library(Labm8_cpp_statusor_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}statusor${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES labm8/cpp) +if(Labm8_INCLUDE_DIRS AND Labm8_cpp_statusor_LIBRARIES) + add_library(Labm8::cpp::statusor UNKNOWN IMPORTED) + set_target_properties(Labm8::cpp::statusor PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Labm8_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${Labm8_cpp_statusor_LIBRARIES}") +endif() + +find_library(Labm8_cpp_logging_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}logging${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES labm8/cpp) +if(Labm8_INCLUDE_DIRS AND Labm8_cpp_logging_LIBRARIES) + add_library(Labm8::cpp::logging UNKNOWN IMPORTED) + set(_LINK_LIBS + Labm8::cpp::string + Labm8::cpp::stringpiece + absl::strings + absl::time) + set_target_properties(Labm8::cpp::logging PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Labm8_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${Labm8_cpp_logging_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES "${_LINK_LIBS}") +endif() + +find_package_handle_standard_args( + Labm8 + REQUIRED_VARS + Labm8_HAS_absl + Labm8_HAS_fmt + Labm8_INCLUDE_DIRS + Labm8_cpp_string_LIBRARIES + Labm8_cpp_stringpiece_LIBRARIES + Labm8_cpp_status_LIBRARIES + Labm8_cpp_statusor_LIBRARIES + Labm8_cpp_logging_LIBRARIES) diff --git a/build_tools/cmake/FindProGraML.cmake b/build_tools/cmake/FindProGraML.cmake new file mode 100644 index 0000000000..a459f25e25 --- /dev/null +++ b/build_tools/cmake/FindProGraML.cmake @@ -0,0 +1,179 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Find ProGraML headers and libraries. + +Imported Targets +^^^^^^^^^^^^^^^^ + +``ProGraML::graph::format::node_link_graph`` +``ProGraML::ir::llvm::llvm-10`` +``ProGraML::proto::programl_cc`` +``ProGraML::graph::program_graph_builder`` + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``ProGraML_FOUND`` + true if ProGraML is available. + + +#]=======================================================================] + +include(FindPackageHandleStandardArgs) + +function(has_Labm8 _RES_VAR) + if(TARGET Labm8::cpp::status AND + TARGET Labm8::cpp::statusor AND + TARGET Labm8::cpp::logging AND + TARGET Labm8::cpp::string AND + TARGET Labm8::cpp::stringpiece) + set(${_RES_VAR} True PARENT_SCOPE) + else() + set(${_RES_VAR} False PARENT_SCOPE) + endif() +endfunction() + +function(has_absl _RES_VAR) + if(TARGET absl::flat_hash_map AND + TARGET absl::flat_hash_set) + set(${_RES_VAR} True PARENT_SCOPE) + else() + set(${_RES_VAR} False PARENT_SCOPE) + endif() +endfunction() + +if(ProGraML_FIND_REQUIRED) + set(_REQUIRED REQUIRED) +endif() + +has_Labm8(ProGraML_HAS_Labm8) +if(NOT ProGraML_HAS_Labm8) + find_package(Labm8 ${_REQUIRED}) + has_Labm8(ProGraML_HAS_Labm8) +endif() + +has_absl(ProGraML_HAS_absl) +if(NOT ProGraML_HAS_absl) + find_package(absl ${_REQUIRED}) + has_absl(ProGraML_HAS_absl) +endif() + +# Deliberately find static libs. +# For some reason the linker takes the path to the library +# instead of just the name for the dynamic section when linking to these libs. +# See https://stackoverflow.com/questions/70088552/linker-adds-the-path-to-library-in-the-dynamic-section-instead-of-its-name +find_library(ProGraML_proto_programl_cc_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}programl${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES programl/proto) +find_path(ProGraML_proto_programl_cc_INCLUDE_DIRS programl/proto/program_graph_options.pb.h) +if (ProGraML_proto_programl_cc_LIBRARIES AND ProGraML_proto_programl_cc_INCLUDE_DIRS) + add_library(ProGraML::proto::programl_cc UNKNOWN IMPORTED) + set_target_properties(ProGraML::proto::programl_cc PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${ProGraML_proto_programl_cc_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${ProGraML_proto_programl_cc_LIBRARIES}") +endif() + +find_library(ProGraML_graph_program_graph_builder_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}program_graph_builder${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES programl/graph/) +find_path(ProGraML_graph_program_graph_builder_INCLUDE_DIRS programl/graph/program_graph_builder.h) +if (ProGraML_graph_program_graph_builder_LIBRARIES AND + ProGraML_graph_program_graph_builder_INCLUDE_DIRS) + set(_INCLUDE_DIRS ${ProGraML_graph_program_graph_builder_INCLUDE_DIRS}) + add_library(ProGraML::graph::program_graph_builder UNKNOWN IMPORTED) + set_target_properties(ProGraML::graph::program_graph_builder PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES + "${ProGraML_graph_program_graph_builder_INCLUDE_DIRS}") + set_target_properties(ProGraML::graph::program_graph_builder PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${ProGraML_graph_program_graph_builder_LIBRARIES}") + set(_LINK_LIBS + ProGraML::proto::programl_cc + absl::flat_hash_map + absl::flat_hash_set + Labm8::cpp::logging + Labm8::cpp::status + Labm8::cpp::statusor + Labm8::cpp::string) + set_target_properties(ProGraML::graph::program_graph_builder + PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES "${_LINK_LIBS}") +endif() + +find_library(ProGraML_graph_features_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}features${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES programl/graph/) +find_path(ProGraML_graph_features_INCLUDE_DIRS programl/graph/features.h) +if (ProGraML_graph_features_LIBRARIES AND + ProGraML_graph_features_INCLUDE_DIRS) + set(_INCLUDE_DIRS ${ProGraML_graph_features_INCLUDE_DIRS}) + add_library(ProGraML::graph::features UNKNOWN IMPORTED) + set_target_properties(ProGraML::graph::features PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${ProGraML_graph_features_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${ProGraML_graph_features_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES ProGraML::proto::programl_cc) +endif() + +find_library(ProGraML_graph_format_node_link_graph_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}node_link_graph${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES programl/graph/format) +find_path(ProGraML_graph_format_node_link_graph_INCLUDE_DIRS programl/graph/format/node_link_graph.h) +if (ProGraML_graph_format_node_link_graph_LIBRARIES AND + ProGraML_graph_format_node_link_graph_INCLUDE_DIRS) + add_library(ProGraML::graph::format::node_link_graph UNKNOWN IMPORTED) + set(_INCLUDE_DIRS ${ProGraML_graph_format_node_link_graph_INCLUDE_DIRS}) + set(_LINK_LIBS + ProGraML::proto::programl_cc + Labm8::cpp::status + Labm8::cpp::logging) + set_target_properties(ProGraML::graph::format::node_link_graph PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${ProGraML_graph_format_node_link_graph_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES "${_LINK_LIBS}" + ) +endif() + +find_library(ProGraML_ir_llvm_llvm_10_LIBRARIES + ${CMAKE_STATIC_LIBRARY_PREFIX}llvm-10${CMAKE_STATIC_LIBRARY_SUFFIX} + PATH_SUFFIXES programl/ir/llvm) +find_path(ProGraML_ir_llvm_llvm_10_INCLUDE_DIRS programl/ir/llvm/llvm.h) +if (ProGraML_ir_llvm_llvm_10_LIBRARIES AND ProGraML_ir_llvm_llvm_10_INCLUDE_DIRS) + add_library(ProGraML::ir::llvm::llvm-10 UNKNOWN IMPORTED) + set(_LINK_LIBS + ProGraML::graph::features + ProGraML::graph::program_graph_builder + ProGraML::proto::programl_cc + absl::flat_hash_map + absl::flat_hash_set + Labm8::cpp::status + Labm8::cpp::statusor + Labm8::cpp::string) + set_target_properties(ProGraML::ir::llvm::llvm-10 PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${ProGraML_ir_llvm_llvm_10_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" + IMPORTED_LOCATION "${ProGraML_ir_llvm_llvm_10_LIBRARIES}" + IMPORTED_LINK_INTERFACE_LIBRARIES "${_LINK_LIBS}") +endif() + +find_package_handle_standard_args(ProGraML + REQUIRED_VARS + ProGraML_HAS_Labm8 + ProGraML_HAS_absl + ProGraML_graph_format_node_link_graph_LIBRARIES + ProGraML_graph_format_node_link_graph_INCLUDE_DIRS + ProGraML_graph_features_LIBRARIES + ProGraML_graph_features_INCLUDE_DIRS + ProGraML_graph_program_graph_builder_INCLUDE_DIRS + ProGraML_graph_program_graph_builder_LIBRARIES + ProGraML_ir_llvm_llvm_10_LIBRARIES + ProGraML_ir_llvm_llvm_10_INCLUDE_DIRS + ProGraML_proto_programl_cc_LIBRARIES + ProGraML_proto_programl_cc_INCLUDE_DIRS) diff --git a/build_tools/cmake/FindSubprocess.cmake b/build_tools/cmake/FindSubprocess.cmake new file mode 100644 index 0000000000..f10729f1e2 --- /dev/null +++ b/build_tools/cmake/FindSubprocess.cmake @@ -0,0 +1,37 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +#[=======================================================================[.rst: +Find Subprocess headers and library. + +Imported Targets +^^^^^^^^^^^^^^^^ + +``Subprocess::libsubprocess`` + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables in your project: + +``Subprocess_FOUND`` + true if Subprocess is available. + + +#]=======================================================================] + +include(FindPackageHandleStandardArgs) + +find_path(Subprocess_INCLUDE_DIRS subprocess/subprocess.hpp) +if (Subprocess_INCLUDE_DIRS) + add_library(Subprocess::libsubprocess INTERFACE IMPORTED) + set_target_properties(Subprocess::libsubprocess PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Subprocess_INCLUDE_DIRS}") +endif() + + +find_package_handle_standard_args(Subprocess + REQUIRED_VARS + Subprocess_INCLUDE_DIRS) diff --git a/build_tools/cmake/build_external_cmake_project.cmake b/build_tools/cmake/build_external_cmake_project.cmake new file mode 100644 index 0000000000..a3ebb94371 --- /dev/null +++ b/build_tools/cmake/build_external_cmake_project.cmake @@ -0,0 +1,53 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include_guard(GLOBAL) +include(CMakeParseArguments) +include(write_cache_script) + +function(build_external_cmake_project) + cmake_parse_arguments( + _RULE + "" + "NAME;SRC_DIR;INSTALL_PREFIX" + "CONFIG_ARGS" + ${ARGN} + ) + + set(_BIN_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/${_RULE_NAME}") + if(_RULE_INSTALL_PREFIX) + set(_INSTALL_PREFIX "${_RULE_INSTALL_PREFIX}") + else() + set(_INSTALL_PREFIX "${_BIN_DIR}/install") + endif() + + set(_INTIAL_CACHE_PATH "${_BIN_DIR}/${_RULE_NAME}_initial_cache.cmake") + write_cache_script("${_INTIAL_CACHE_PATH}") + + execute_process( + COMMAND "${CMAKE_COMMAND}" + -G "${CMAKE_GENERATOR}" # For some reason the generator is not taken from the initial cache. + -C "${_INTIAL_CACHE_PATH}" + -S "${_RULE_SRC_DIR}" + -B "${_BIN_DIR}" + -D "CMAKE_INSTALL_PREFIX=${_INSTALL_PREFIX}" + ${_RULE_CONFIG_ARGS} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" + --build "${_BIN_DIR}" + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" + --install "${_BIN_DIR}" + COMMAND_ERROR_IS_FATAL ANY + ) + list(PREPEND CMAKE_PREFIX_PATH "${_INSTALL_PREFIX}") + set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE) +endfunction() diff --git a/build_tools/cmake/cg_add_all_subdirs.cmake b/build_tools/cmake/cg_add_all_subdirs.cmake new file mode 100644 index 0000000000..5a7cd2f021 --- /dev/null +++ b/build_tools/cmake/cg_add_all_subdirs.cmake @@ -0,0 +1,32 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# === +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_add_all_subdirs.cmake +# Copyright 2020 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# cg_add_all_subidrs +# +# CMake function to add all subdirectories of the current directory that contain +# a CMakeLists.txt file +# +# Takes no arguments. +function(cg_add_all_subdirs) + FILE(GLOB _CHILDREN RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*) + SET(_DIRLIST "") + foreach(_CHILD ${_CHILDREN}) + if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${_CHILD} AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_CHILD}/CMakeLists.txt) + LIST(APPEND _DIRLIST ${_CHILD}) + endif() + endforeach() + + foreach(subdir ${_DIRLIST}) + add_subdirectory(${subdir}) + endforeach() +endfunction() diff --git a/build_tools/cmake/cg_cc_binary.cmake b/build_tools/cmake/cg_cc_binary.cmake new file mode 100644 index 0000000000..6eb3771958 --- /dev/null +++ b/build_tools/cmake/cg_cc_binary.cmake @@ -0,0 +1,148 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_binary.cmake[ +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(CMakeParseArguments) + +# cg_cc_binary() +# +# CMake function to imitate Bazel's cc_binary rule. +# +# Parameters: +# NAME: name of target (see Usage below) +# SRCS: List of source files for the binary +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# COPTS: List of private compile options +# DEFINES: List of public defines +# LINKOPTS: List of link options +# TESTONLY: for testing; won't compile when tests are disabled +# HOSTONLY: host only; compile using host toolchain when cross-compiling +# +# Note: +# cg_cc_binary will create a binary called ${PACKAGE_NAME}_${NAME}, e.g. +# cmake_base_foo with two alias (readonly) targets, a qualified +# ${PACKAGE_NS}::${NAME} and an unqualified ${NAME}. Thus NAME must be globally +# unique in the project. +# +# Usage: +# cg_cc_library( +# NAME +# awesome +# HDRS +# "a.h" +# SRCS +# "a.cc" +# PUBLIC +# ) +# +# cg_cc_binary( +# NAME +# awesome_tool +# SRCS +# "awesome-tool-main.cc" +# DEPS +# compiler_gym::awesome +# ) +function(cg_cc_binary) + cmake_parse_arguments( + _RULE + "HOSTONLY;TESTONLY" + "NAME" + "SRCS;COPTS;DEFINES;LINKOPTS;DATA;DEPS;ABS_DEPS;INCLUDES" + ${ARGN} + ) + + if(_RULE_TESTONLY AND NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + cg_package_ns(_PACKAGE_NS) + # Prefix the library with the package name, so we get: cg_package_name + rename_bazel_targets(_NAME "${_RULE_NAME}") + + add_executable(${_NAME} "") + add_executable(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME}) + + # If the binary name matches the package then treat it as a default. For + # example, foo/bar/ library 'bar' would end up as 'foo::bar'. This isn't + # likely to be common for binaries, but is consistent with the behavior for + # libraries and in Bazel. + cg_package_dir(_PACKAGE_DIR) + if(${_RULE_NAME} STREQUAL ${_PACKAGE_DIR}) + add_executable(${_PACKAGE_NS} ALIAS ${_NAME}) + endif() + + # Finally, since we have so few binaries and we also want to support + # installing from a separate host build, binaries get an unqualified global + # alias. This means binary names must be unique across the whole project. + # (We could consider making this configurable). + add_executable(${_RULE_NAME} ALIAS ${_NAME}) + + set_target_properties(${_NAME} PROPERTIES OUTPUT_NAME "${_RULE_NAME}") + if(_RULE_SRCS) + target_sources(${_NAME} + PRIVATE + ${_RULE_SRCS} + ) + else() + set(_DUMMY_SRC "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_dummy.cc") + file(WRITE ${_DUMMY_SRC} "") + target_sources(${_NAME} + PRIVATE + ${_DUMMY_SRC} + ) + endif() + target_include_directories(${_NAME} SYSTEM + PUBLIC + "$" + "$" + ) + target_include_directories(${_NAME} + PUBLIC + "$" + ) + target_compile_definitions(${_NAME} + PUBLIC + ${_RULE_DEFINES} + ) + target_compile_options(${_NAME} + PRIVATE + ${COMPILER_GYM_DEFAULT_COPTS} + ${_RULE_COPTS} + ) + target_link_options(${_NAME} + PRIVATE + ${COMPILER_GYM_DEFAULT_LINKOPTS} + ${_RULE_LINKOPTS} + ) + + rename_bazel_targets(_RULE_DEPS "${_RULE_DEPS}") + + target_link_libraries(${_NAME} + PUBLIC + ${_RULE_DEPS} + ${_RULE_ABS_DEPS} + ) + + cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA}) + + # Add all targets to a folder in the IDE for organization. + set_target_properties(${_NAME} PROPERTIES + FOLDER ${COMPILER_GYM_IDE_FOLDER}/binaries + CXX_STANDARD ${COMPILER_GYM_CXX_STANDARD} + CXX_STANDARD_REQUIRED ON) + + install(TARGETS ${_NAME} + RENAME ${_RULE_NAME} + COMPONENT ${_RULE_NAME} + RUNTIME DESTINATION bin) +endfunction() diff --git a/build_tools/cmake/cg_cc_library.cmake b/build_tools/cmake/cg_cc_library.cmake new file mode 100644 index 0000000000..dfcd066d6d --- /dev/null +++ b/build_tools/cmake/cg_cc_library.cmake @@ -0,0 +1,192 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_library.cmake +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(CMakeParseArguments) + +# cg_cc_library() +# +# CMake function to imitate Bazel's cc_library rule. +# +# Parameters: +# NAME: name of target (see Note) +# HDRS: List of public header files for the library +# TEXTUAL_HDRS: List of public header files that cannot be compiled on their own +# SRCS: List of source files for the library +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# COPTS: List of private compile options +# DEFINES: List of public defines +# INCLUDES: Include directories to add to dependencies +# LINKOPTS: List of link options +# Also in IDE, target will appear in IREE folder while non PUBLIC will be in IREE/internal. +# TESTONLY: When added, this target will only be built if user passes -DCOMPILER_GYM_BUILD_TESTS=ON to CMake. +# SHARED: If set, will compile to a shared object. +# +# cg_cc_library( +# NAME +# awesome +# HDRS +# "a.h" +# SRCS +# "a.cc" +# ) +# cg_cc_library( +# NAME +# fantastic_lib +# SRCS +# "b.cc" +# DEPS +# package::awesome # not "awesome" ! +# PUBLIC +# ) +# +# cg_cc_library( +# NAME +# main_lib +# ... +# DEPS +# package::fantastic_lib +# ) +function(cg_cc_library) + cmake_parse_arguments( + _RULE + "PUBLIC;TESTONLY;SHARED" + "NAME" + "HDRS;TEXTUAL_HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DATA;DEPS;ABS_DEPS;NON_LIB_DEPS;INCLUDES" + ${ARGN} + ) + + if(_RULE_TESTONLY AND NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + cg_package_ns(_PACKAGE_NS) + rename_bazel_targets(_DEPS "${_RULE_DEPS}") + list(APPEND _DEPS ${_RULE_ABS_DEPS}) + + # Prefix the library with the package name, so we get: cg_package_name. + rename_bazel_targets(_NAME "${_RULE_NAME}") + + # Check if this is a header-only library. + # Note that as of February 2019, many popular OS's (for example, Ubuntu + # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't + # use list(FILTER...) + set(_CC_SRCS "${_RULE_SRCS}") + foreach(src_file IN LISTS _CC_SRCS) + if(${src_file} MATCHES ".*\\.(h|inc)") + list(REMOVE_ITEM _CC_SRCS "${src_file}") + endif() + endforeach() + if("${_CC_SRCS}" STREQUAL "") + set(_RULE_IS_INTERFACE 1) + else() + set(_RULE_IS_INTERFACE 0) + endif() + + if(NOT _RULE_IS_INTERFACE) + if(_RULE_SHARED) + add_library(${_NAME} SHARED "") + else() + add_library(${_NAME} STATIC "") + endif() + if(_RULE_SRCS) + list(JOIN _RULE_SRCS ";\n" SRCSTR) + message("${SRCSTR}") + endif() + target_sources(${_NAME} + PRIVATE + ${_RULE_SRCS} + ${_RULE_TEXTUAL_HDRS} + ${_RULE_HDRS} + ) + target_include_directories(${_NAME} SYSTEM + PUBLIC + "$" + "$" + ) + target_include_directories(${_NAME} + PUBLIC + "$" + ${_RULE_INCLUDES} + ) + target_compile_options(${_NAME} + PRIVATE + ${COMPILER_GYM_DEFAULT_COPTS} + ${_RULE_COPTS} + ) + target_link_options(${_NAME} + PRIVATE + ${COMPILER_GYM_DEFAULT_LINKOPTS} + ${_RULE_LINKOPTS} + ) + target_link_libraries(${_NAME} + PUBLIC + ${_DEPS} + ) + + target_compile_definitions(${_NAME} + PUBLIC + ${_RULE_DEFINES} + ) + + # Add all targets to a folder in the IDE for organization. + if(_RULE_PUBLIC) + set_property(TARGET ${_NAME} PROPERTY FOLDER ${COMPILER_GYM_IDE_FOLDER}) + elseif(_RULE_TESTONLY) + set_property(TARGET ${_NAME} PROPERTY FOLDER ${COMPILER_GYM_IDE_FOLDER}/test) + else() + set_property(TARGET ${_NAME} PROPERTY FOLDER ${COMPILER_GYM_IDE_FOLDER}/internal) + endif() + + # INTERFACE libraries can't have the CXX_STANDARD property set. + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${COMPILER_GYM_CXX_STANDARD}) + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) + else() + # Generating header-only library. + add_library(${_NAME} INTERFACE ${_RULE_SRCS} ${_RULE_TEXTUAL_HDRS} ${_RULE_HDRS}) + target_include_directories(${_NAME} SYSTEM + INTERFACE + "$" + "$" + ) + target_link_options(${_NAME} + INTERFACE + ${COMPILER_GYM_DEFAULT_LINKOPTS} + ${_RULE_LINKOPTS} + ) + target_link_libraries(${_NAME} + INTERFACE + ${_DEPS} + ) + target_compile_definitions(${_NAME} + INTERFACE + ${_RULE_DEFINES} + ) + endif() + + cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA}) + + if (_RULE_NON_LIB_DEPS) + rename_bazel_targets(_NON_LIB_DEPS "${_RULE_NON_LIB_DEPS}") + add_dependencies(${_NAME} ${_NON_LIB_DEPS}) + endif() + + add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME}) + + # If the library name matches the final component of the package then treat + # it as a default. For example, foo/bar/ library 'bar' would end up as + # 'foo::bar'. + cg_package_dir(_PACKAGE_DIR) + if(${_RULE_NAME} STREQUAL ${_PACKAGE_DIR}) + add_library(${_PACKAGE_NS} ALIAS ${_NAME}) + endif() +endfunction() diff --git a/build_tools/cmake/cg_cc_test.cmake b/build_tools/cmake/cg_cc_test.cmake new file mode 100644 index 0000000000..d0df3b48c0 --- /dev/null +++ b/build_tools/cmake/cg_cc_test.cmake @@ -0,0 +1,96 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_test.cmake +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(CMakeParseArguments) +include(cg_installed_test) + +# cg_cc_test() +# +# CMake function to imitate Bazel's cc_test rule. +# +# Parameters: +# NAME: name of target. This name is used for the generated executable and +# SRCS: List of source files for the binary +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# COPTS: List of private compile options +# DEFINES: List of public defines +# LINKOPTS: List of link options +# LABELS: Additional labels to apply to the test. The package path is added +# automatically. +# +# Note: +# cg_cc_test will create a binary called ${PACKAGE_NAME}_${NAME}, e.g. +# cg_base_foo_test. +# +# +# Usage: +# cg_cc_library( +# NAME +# awesome +# HDRS +# "a.h" +# SRCS +# "a.cc" +# PUBLIC +# ) +# +# cg_cc_test( +# NAME +# awesome_test +# SRCS +# "awesome_test.cc" +# DEPS +# gtest_main +# compiler_gym::awesome +# ) +function(cg_cc_test) + if(NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + cmake_parse_arguments( + _RULE + "" + "NAME" + "SRCS;COPTS;DEFINES;LINKOPTS;DATA;DEPS;LABELS" + ${ARGN} + ) + + cg_cc_binary(${ARGV}) + + rename_bazel_targets(_NAME "${_RULE_NAME}") + cg_package_ns(_PACKAGE_NS) + string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS}) + set(_TEST_NAME "${_PACKAGE_PATH}/${_RULE_NAME}") + set(_LABELS "${_RULE_LABELS}") + list(APPEND _LABELS "${_PACKAGE_PATH}") + + cg_add_installed_test( + TEST_NAME "${_TEST_NAME}" + LABELS "${_LABELS}" + COMMAND + # We run all our tests through a custom test runner to allow temp + # directory cleanup upon test completion. + "${CMAKE_SOURCE_DIR}/build_tools/cmake/run_test.${COMPILER_GYM_HOST_SCRIPT_EXT}" + "$" + INSTALLED_COMMAND + # Must match install destination below. + "${_PACKAGE_PATH}/$" + ) + + install(TARGETS ${_NAME} + DESTINATION "tests/${_PACKAGE_PATH}" + COMPONENT Tests + ) + +endfunction() diff --git a/build_tools/cmake/cg_copts.cmake b/build_tools/cmake/cg_copts.cmake new file mode 100644 index 0000000000..9ae5edd69f --- /dev/null +++ b/build_tools/cmake/cg_copts.cmake @@ -0,0 +1,124 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#------------------------------------------------------------------------------- +# C/C++ options as used within Compiler Gym +#------------------------------------------------------------------------------- +# +# ██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████ +# ██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██ +# ██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███ +# ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +# ███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████ +# +# Everything here is added to *every* cg_cc_library/cg_cc_binary/etc. +# That includes both runtime and compiler components, and these may propagate +# out to user code interacting with either (such as custom modules). +# +# Be extremely judicious in the use of these flags. +# +# - Need to disable a warning? +# Usually these are encountered in compiler-specific code and can be disabled +# in a compiler-specific way. Only add global warning disables when it's clear +# that we never want them or that they'll show up in a lot of places. +# +# See: https://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code +# +# - Need to add a linker dependency? +# First figure out if you *really* need it. If it's only required on specific +# platforms and in very specific files clang or msvc are used prefer +# autolinking. GCC is stubborn and doesn't have autolinking so additional +# flags may be required there. +# +# See: https://en.wikipedia.org/wiki/Auto-linking + + +set(COMPILER_GYM_CXX_STANDARD ${CMAKE_CXX_STANDARD}) + +# TODO(benvanik): fix these names (or remove entirely). +set(COMPILER_GYM_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(COMPILER_GYM_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(COMPILER_GYM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) + +# Compiler diagnostics. +cg_select_compiler_opts(COMPILER_GYM_DEFAULT_COPTS + # Clang diagnostics. These largely match the set of warnings used within + # Google. They have not been audited super carefully by the IREE team but are + # generally thought to be a good set and consistency with those used + # internally is very useful when importing. If you feel that some of these + # should be different (especially more strict), please raise an issue! + CLANG + "-Werror" + "-Wall" + + # Disable warnings we don't care about or that generally have a low + # signal/noise ratio. + "-Wno-ambiguous-member-template" + "-Wno-char-subscripts" + "-Wno-deprecated-declarations" + "-Wno-extern-c-compat" # Matches upstream. Cannot impact due to extern C inclusion method. + "-Wno-gnu-alignof-expression" + "-Wno-gnu-variable-sized-type-not-at-end" + "-Wno-ignored-optimization-argument" + "-Wno-invalid-offsetof" # Technically UB but needed for intrusive ptrs + "-Wno-invalid-source-encoding" + "-Wno-mismatched-tags" + "-Wno-pointer-sign" + "-Wno-reserved-user-defined-literal" + "-Wno-return-type-c-linkage" + "-Wno-self-assign-overloaded" + "-Wno-sign-compare" + "-Wno-signed-unsigned-wchar" + "-Wno-strict-overflow" + "-Wno-trigraphs" + "-Wno-unknown-pragmas" + "-Wno-unknown-warning-option" + "-Wno-unused-command-line-argument" + "-Wno-unused-const-variable" + "-Wno-unused-function" + "-Wno-unused-local-typedef" + "-Wno-unused-private-field" + "-Wno-user-defined-warnings" + + # Explicitly enable some additional warnings. + # Some of these aren't on by default, or under -Wall, or are subsets of + # warnings turned off above. + "-Wctad-maybe-unsupported" + "-Wfloat-overflow-conversion" + "-Wfloat-zero-conversion" + "-Wfor-loop-analysis" + "-Wformat-security" + "-Wgnu-redeclared-enum" + "-Wimplicit-fallthrough" + "-Winfinite-recursion" + "-Wliteral-conversion" + #"-Wnon-virtual-dtor" + "-Woverloaded-virtual" + "-Wself-assign" + "-Wstring-conversion" + "-Wtautological-overlap-compare" + "-Wthread-safety" + "-Wthread-safety-beta" + "-Wunused-comparison" + "-Wvla" + + # TODO(#6959): Enable -Werror once we have a presubmit CI. + GCC + "-Wall" + "-Wno-address-of-packed-member" + "-Wno-comment" + "-Wno-format-zero-length" + # Technically UB but needed for intrusive ptrs + $<$:-Wno-invalid-offsetof> + $<$:-Wno-pointer-sign> + "-Wno-sign-compare" + "-Wno-unused-function" +) diff --git a/build_tools/cmake/cg_filegroup.cmake b/build_tools/cmake/cg_filegroup.cmake new file mode 100644 index 0000000000..5a12d01875 --- /dev/null +++ b/build_tools/cmake/cg_filegroup.cmake @@ -0,0 +1,51 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +function(cg_filegroup) + cmake_parse_arguments( + _ARG + "PUBLIC" + "NAME" + "FILES;DEPENDS" + ${ARGN} + ) + rename_bazel_targets(_NAME "${_ARG_NAME}") + add_custom_target(${_NAME}) + + foreach(FILE_ ${_ARG_FILES}) + if(IS_ABSOLUTE "${FILE_}") + set(_INPUT_PATH "${FILE_}") + get_filename_component(_FILE_NAME ${FILE_} NAME) + canonize_bazel_target_names(_FILE_TARGET "${_FILE_NAME}") + rename_bazel_targets(_TARGET "${_FILE_TARGET}") + set(_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_FILE_NAME}") + else() + canonize_bazel_target_names(_FILE_TARGET "${FILE_}") + rename_bazel_targets(_TARGET "${_FILE_TARGET}") + set(_INPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${FILE_}") + set(_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/${FILE_}") + endif() + + if(NOT TARGET ${_TARGET}) + if (NOT _INPUT_PATH STREQUAL _OUTPUT_PATH) + add_custom_command(OUTPUT "${_OUTPUT_PATH}" + COMMAND ${CMAKE_COMMAND} -E create_symlink "${_INPUT_PATH}" "${_OUTPUT_PATH}" + DEPENDS "${_INPUT_PATH}") + endif() + add_custom_target(${_TARGET} DEPENDS "${_OUTPUT_PATH}") + endif() + + add_dependencies(${_NAME} ${_TARGET}) + endforeach() + + if(_ARG_DEPENDS) + rename_bazel_targets(_DEPS "${_ARG_DEPENDS}") + add_dependencies(${_NAME} ${_DEPS}) + endif() + + set_target_properties(${_NAME} PROPERTIES + IS_FILEGROUP TRUE + OUTPUTS "${_SRCS}") +endfunction() diff --git a/build_tools/cmake/cg_genrule.cmake b/build_tools/cmake/cg_genrule.cmake new file mode 100644 index 0000000000..cacae5bcad --- /dev/null +++ b/build_tools/cmake/cg_genrule.cmake @@ -0,0 +1,73 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include_guard(GLOBAL) + +include(CMakeParseArguments) +include(cg_macros) + +# cg_genrule() +# +# CMake function to imitate Bazel's genrule rule. +# +function(cg_genrule) + cmake_parse_arguments( + _RULE + "PUBLIC;TESTONLY" + "NAME;COMMAND" + "SRCS;OUTS;DEPENDS;ABS_DEPENDS" + ${ARGN} + ) + + if(_RULE_TESTONLY AND NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + # TODO(boian): remove this renaming when call sites do not include ":" in target dependency names + rename_bazel_targets(_DEPS "${_RULE_DEPENDS}") + + rename_bazel_targets(_NAME "${_RULE_NAME}") + + make_paths_absolute( + PATHS ${_RULE_SRCS} + BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" + RESULT_VARIABLE _SRCS + ) + + make_paths_absolute( + PATHS ${_RULE_OUTS} + BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}" + RESULT_VARIABLE _OUTS + ) + + list(LENGTH _OUTS _OUTS_LENGTH) + if(_OUTS_LENGTH EQUAL 1) + get_filename_component(_OUTS_DIR "${_OUTS}" DIRECTORY) + else() + set(_OUTS_DIR "${CMAKE_CURRENT_BINARY_DIR}") + endif() + + # Substitute special Bazel references + string(REPLACE "$@" "${_OUTS}" _CMD "${_RULE_COMMAND}") + string(REPLACE "$(@D)" "${_OUTS_DIR}" _CMD "${_CMD}") + #string(REPLACE "$<" "\"${_SRCS}\"" _CMD "${_CMD}") + + add_custom_command( + OUTPUT ${_OUTS} + COMMAND bash -c "${_CMD}" + DEPENDS ${_DEPS} ${_SRCS} + VERBATIM + ) + + add_custom_target(${_NAME} ALL DEPENDS ${_OUTS}) + set_target_properties(${_NAME} PROPERTIES + OUTPUTS "${_OUTS}") + + list(LENGTH _OUTS _OUTS_LENGTH) + if(_OUTS_LENGTH EQUAL "1") + set_target_properties(${_NAME} PROPERTIES LOCATION "${_OUTS}") + endif() + +endfunction() diff --git a/build_tools/cmake/cg_installed_test.cmake b/build_tools/cmake/cg_installed_test.cmake new file mode 100644 index 0000000000..3f9e9c0779 --- /dev/null +++ b/build_tools/cmake/cg_installed_test.cmake @@ -0,0 +1,97 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copyright 2020 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# cg_add_installed_test() +# +# Creates a build-time and exported install-time test. All tests are installed +# into the tests/ tree. Calling code must arrange to install dependencies of the +# test into that tree. +# +# Parameters: +# TEST_NAME: Name of the test (as in "some/path/to/test"). +# COMMAND: Passed to add_test() as is. +# ENVIRONMENT: Set as the ENVIRONMENT property of the build-time test. +# INSTALLED_COMMAND: Corrollary to the 'COMMAND' argument but added to the +# install time definition. +# WORKING_DIRECTORY: Passed to add_test() as is. Note that in the install tree +# all tests run in the tests/ directory. +# LABELS: Labels to pass to add_test() and installed tests. +function(cg_add_installed_test) + cmake_parse_arguments( + _RULE + "" + "TEST_NAME" + "COMMAND;ENVIRONMENT;INSTALLED_COMMAND;WORKING_DIRECTORY;LABELS" + ${ARGN} + ) + + + add_test( + NAME + ${_RULE_TEST_NAME} + COMMAND + ${_RULE_COMMAND} + ) + if (DEFINED _RULE_WORKING_DIRECTORY) + set_property( + TEST + ${_RULE_TEST_NAME} + PROPERTY WORKING_DIRECTORY + "${_RULE_WORKING_DIRECTORY}" + ) + endif() + set_property( + TEST + ${_RULE_TEST_NAME} + PROPERTY LABELS + "${_RULE_LABELS}" + ) + set_property( + TEST + ${_RULE_TEST_NAME} + PROPERTY ENVIRONMENT + "TEST_TMPDIR=${CMAKE_BINARY_DIR}/${_RULE_TEST_NAME}_test_tmpdir" + ${_RULE_ENVIRONMENT} + ) + cg_add_test_environment_properties(${_RULE_TEST_NAME}) + + # Write the to the installed ctest file template. + set(_installed_ctest_input_file + "${CMAKE_BINARY_DIR}/cg_installed_tests.cmake.in") + get_property(_has_tests GLOBAL PROPERTY COMPILER_GYM_HAS_INSTALLED_TESTS) + if(NOT _has_tests) + # First time. + file(WRITE "${_installed_ctest_input_file}") # Truncate. + set_property(GLOBAL PROPERTY COMPILER_GYM_HAS_INSTALLED_TESTS ON) + endif() + + # Now write directives to the installed tests cmake file. + file(APPEND "${_installed_ctest_input_file}" + "add_test(${_RULE_TEST_NAME} ${_RULE_INSTALLED_COMMAND})\n" + "set_tests_properties(${_RULE_TEST_NAME} PROPERTIES LABELS \"${_RULE_LABELS}\")\n" + ) + + # First time generation and setup to install. Note that since this all runs + # at the generate phase, it doesn't matter that we trigger it before all + # tests accumulate. + if(NOT _has_tests) + set(_installed_ctest_output_file "${CMAKE_BINARY_DIR}/cg_installed_tests.cmake") + file(GENERATE + OUTPUT "${_installed_ctest_output_file}" + INPUT "${_installed_ctest_input_file}" + ) + install(FILES "${_installed_ctest_output_file}" + DESTINATION tests + RENAME "CTestTestfile.cmake" + COMPONENT Tests + ) + endif() +endfunction() diff --git a/build_tools/cmake/cg_macros.cmake b/build_tools/cmake/cg_macros.cmake new file mode 100644 index 0000000000..28e0cdd984 --- /dev/null +++ b/build_tools/cmake/cg_macros.cmake @@ -0,0 +1,353 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include_guard(GLOBAL) +include(CMakeParseArguments) + +#------------------------------------------------------------------------------- +# Missing CMake Variables +#------------------------------------------------------------------------------- + +if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") + set(COMPILER_GYM_HOST_SCRIPT_EXT "bat") + # https://gitlab.kitware.com/cmake/cmake/-/issues/17553 + set(COMPILER_GYM_HOST_EXECUTABLE_SUFFIX ".exe") +else() + set(COMPILER_GYM_HOST_SCRIPT_EXT "sh") + set(COMPILER_GYM_HOST_EXECUTABLE_SUFFIX "") +endif() + +#------------------------------------------------------------------------------- +# General utilities +#------------------------------------------------------------------------------- + +# cg_to_bool +# +# Sets `variable` to `ON` if `value` is true and `OFF` otherwise. +function(cg_to_bool VARIABLE VALUE) + if(VALUE) + set(${VARIABLE} "ON" PARENT_SCOPE) + else() + set(${VARIABLE} "OFF" PARENT_SCOPE) + endif() +endfunction() + +# cg_append_list_to_string +# +# Joins ${ARGN} together as a string separated by " " and appends it to +# ${VARIABLE}. +function(cg_append_list_to_string VARIABLE) + if(NOT "${ARGN}" STREQUAL "") + string(JOIN " " _ARGN_STR ${ARGN}) + set(${VARIABLE} "${${VARIABLE}} ${_ARGN_STR}" PARENT_SCOPE) + endif() +endfunction() + + +#------------------------------------------------------------------------------- +# Packages and Paths +#------------------------------------------------------------------------------- + +# Sets ${PACKAGE_NS} to the root relative package name in C++ namespace +# format (::). +# +# Example when called from proj/base/CMakeLists.txt: +# proj::base +function(cg_package_ns PACKAGE_NS) + string(REPLACE ${COMPILER_GYM_ROOT_DIR} "" _PACKAGE ${CMAKE_CURRENT_LIST_DIR}) + string(SUBSTRING ${_PACKAGE} 1 -1 _PACKAGE) + string(REPLACE "/" "::" _PACKAGE_NS ${_PACKAGE}) + set(${PACKAGE_NS} ${_PACKAGE_NS} PARENT_SCOPE) +endfunction() + +# Sets ${PACKAGE_NAME} to the root relative package name. +# +# Example when called from proj/base/CMakeLists.txt: +# proj__base +function(cg_package_name PACKAGE_NAME) + cg_package_ns(_PACKAGE_NS) + string(REPLACE "::" "__" _PACKAGE_NAME ${_PACKAGE_NS}) + set(${PACKAGE_NAME} ${_PACKAGE_NAME} PARENT_SCOPE) +endfunction() + +# Sets ${PACKAGE_PATH} to the root relative package path. +# +# Example when called from proj/base/CMakeLists.txt: +# proj/base +function(cg_package_path PACKAGE_PATH) + cg_package_ns(_PACKAGE_NS) + string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS}) + set(${PACKAGE_PATH} ${_PACKAGE_PATH} PARENT_SCOPE) +endfunction() + +# Sets ${PACKAGE_DIR} to the directory name of the current package. +# +# Example when called from proj/base/CMakeLists.txt: +# base +function(cg_package_dir PACKAGE_DIR) + cg_package_ns(_PACKAGE_NS) + string(FIND ${_PACKAGE_NS} "::" _END_OFFSET REVERSE) + math(EXPR _END_OFFSET "${_END_OFFSET} + 2") + string(SUBSTRING ${_PACKAGE_NS} ${_END_OFFSET} -1 _PACKAGE_DIR) + set(${PACKAGE_DIR} ${_PACKAGE_DIR} PARENT_SCOPE) +endfunction() + +function(canonize_bazel_target_names _RESULT _BAZEL_TARGETS) + unset(_RES) + cg_package_ns(_PACKAGE_NS) + foreach(_TARGET ${_BAZEL_TARGETS}) + if (NOT _TARGET MATCHES ":") + # local target + set(_TARGET "${_PACKAGE_NS}::${_TARGET}") + endif() + list(APPEND _RES "${_TARGET}") + endforeach() + list(TRANSFORM _RES REPLACE "^::" "${_PACKAGE_NS}::") + set(${_RESULT} ${_RES} PARENT_SCOPE) +endfunction() + +function(rename_bazel_targets _RESULT _BAZEL_TARGETS) + canonize_bazel_target_names(_RES "${_BAZEL_TARGETS}") + list(TRANSFORM _RES REPLACE ":" "_") + set(${_RESULT} ${_RES} PARENT_SCOPE) +endfunction() + +function(get_target_as_relative_dir _TARGET _RESULT) + set(_RES "${_TARGET}") + list(TRANSFORM _RES REPLACE "__" "/") + get_filename_component(_RES "${_RES}" DIRECTORY) + set(${_RESULT} "${_RES}" PARENT_SCOPE) +endfunction() + +function(get_target_out_cxx_header_dir _TARGET _RESULT) + get_target_property(_BIN_DIR ${_TARGET} BINARY_DIR) + get_target_as_relative_dir(${_TARGET} _REL_HEADER_DIR) + set(${_RESULT} "${_BIN_DIR}/include/${_REL_HEADER_DIR}" PARENT_SCOPE) +endfunction() + +function(make_paths_absolute) + cmake_parse_arguments( + _ARG + "" + "BASE_DIR;RESULT_VARIABLE" + "PATHS" + ${ARGN} + ) + + unset(_RES) + foreach(_PATH ${_ARG_PATHS}) + if(NOT IS_ABSOLUTE _PATH) + get_filename_component(_PATH "${_PATH}" ABSOLUTE BASE_DIR "${_ARG_BASE_DIR}") + endif() + list(APPEND _RES "${_PATH}") + endforeach() + + set(${_ARG_RESULT_VARIABLE} "${_RES}" PARENT_SCOPE) +endfunction() + +function(paths_to_targets) + cmake_parse_arguments( + _ARG + "" + "RESULT" + "PATHS" + ${ARGN} + ) + + string(REGEX REPLACE "[^A-Za-z0-9_+-]" "_" _TARGETS "${_ARG_PATHS}") + set(${_ARG_RESULT} "${_TARGETS}" PARENT_SCOPE) +endfunction() + +#------------------------------------------------------------------------------- +# select()-like Evaluation +#------------------------------------------------------------------------------- + +# Appends ${OPTS} with a list of values based on the current compiler. +# +# Example: +# cg_select_compiler_opts(COPTS +# CLANG +# "-Wno-foo" +# "-Wno-bar" +# CLANG_CL +# "/W3" +# GCC +# "-Wsome-old-flag" +# MSVC +# "/W3" +# ) +# +# Note that variables are allowed, making it possible to share options between +# different compiler targets. +function(cg_select_compiler_opts OPTS) + cmake_parse_arguments( + PARSE_ARGV 1 + _COMPILER_GYM_SELECTS + "" + "" + "ALL;CLANG;CLANG_CL;MSVC;GCC;CLANG_OR_GCC;MSVC_OR_CLANG_CL" + ) + # OPTS is a variable containing the *name* of the variable being populated, so + # we need to dereference it twice. + set(_OPTS "${${OPTS}}") + list(APPEND _OPTS "${_COMPILER_GYM_SELECTS_ALL}") + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + list(APPEND _OPTS "${_COMPILER_GYM_SELECTS_GCC}") + list(APPEND _OPTS "${_COMPILER_GYM_SELECTS_CLANG_OR_GCC}") + elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") + if(MSVC) + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_CLANG_CL}) + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_MSVC_OR_CLANG_CL}) + else() + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_CLANG}) + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_CLANG_OR_GCC}) + endif() + elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_MSVC}) + list(APPEND _OPTS ${_COMPILER_GYM_SELECTS_MSVC_OR_CLANG_CL}) + else() + message(ERROR "Unknown compiler: ${CMAKE_CXX_COMPILER}") + list(APPEND _OPTS "") + endif() + set(${OPTS} ${_OPTS} PARENT_SCOPE) +endfunction() + +#------------------------------------------------------------------------------- +# Data dependencies +#------------------------------------------------------------------------------- + +# Adds 'data' dependencies to a target. +# +# Parameters: +# NAME: name of the target to add data dependencies to +# DATA: List of targets and/or files in the source tree. Files should use the +# same format as targets (i.e. iree::package::subpackage::file.txt) +function(cg_add_data_dependencies) + cmake_parse_arguments( + _RULE + "" + "NAME" + "DATA" + ${ARGN} + ) + # TODO(boian): Make runtime targets that depend on data + + if(NOT DEFINED _RULE_DATA) + return() + endif() + + rename_bazel_targets(_NAME "${_RULE_NAME}") + unset(_DEPS) + + foreach(_DATA ${_RULE_DATA}) + if(IS_ABSOLUTE "${_DATA}") + get_filename_component(FILE_ "${_DATA}" ABSOLUTE) + paths_to_targets(PATHS "${FILE_}" RESULT _TARGET) + string(PREPEND _TARGET "${_NAME}_data_") + get_filename_component(_FILE_NAME "${FILE_}" NAME) + set(_DST_DIR "${CMAKE_CURRENT_BINARY_DIR}") + set(_DST_PATH "${_DST_DIR}/${_FILE_NAME}") + if(NOT _DST_PATH STREQUAL _DATA) + add_custom_command( + OUTPUT "${_DST_PATH}" + COMMAND + ${CMAKE_COMMAND} -E make_directory "${_DST_DIR}" + COMMAND ${CMAKE_COMMAND} -E create_symlink + "${FILE_}" "${_DST_PATH}" + DEPENDS "${FILE_}" + VERBATIM + ) + endif() + add_custom_target(${_TARGET} DEPENDS "${_DST_PATH}") + else() + rename_bazel_targets(_TARGET "${_DATA}") + endif() + list(APPEND _DEPS "${_TARGET}") + endforeach() + + add_dependencies(${_NAME} ${_DEPS}) +endfunction() + +#------------------------------------------------------------------------------- +# Tool symlinks +#------------------------------------------------------------------------------- + +# cg_symlink_tool +# +# Adds a command to TARGET which symlinks a tool from elsewhere +# (FROM_TOOL_TARGET_NAME) to a local file name (TO_EXE_NAME) in the current +# binary directory. +# +# Parameters: +# TARGET: Local target to which to add the symlink command (i.e. an +# cg_py_library, etc). +# FROM_TOOL_TARGET: Target of the tool executable that is the source of the +# link. +# TO_EXE_NAME: The executable name to output in the current binary dir. +function(cg_symlink_tool) + cmake_parse_arguments( + ARG + "" + "TARGET;FROM_TOOL_TARGET;TO_EXE_NAME" + "" + ${ARGN} + ) + + # Transform TARGET + cg_package_ns(_PACKAGE_NS) + cg_package_name(_PACKAGE_NAME) + set(_TARGET "${_PACKAGE_NAME}_${ARG_TARGET}") + set(_FROM_TOOL_TARGET ${ARG_FROM_TOOL_TARGET}) + set(_TO_TOOL_PATH "${CMAKE_CURRENT_BINARY_DIR}/${ARG_TO_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}") + get_filename_component(_TO_TOOL_DIR "${_TO_TOOL_PATH}" DIRECTORY) + + + add_custom_command( + TARGET "${_TARGET}" + BYPRODUCTS + "${CMAKE_CURRENT_BINARY_DIR}/${ARG_TO_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}" + COMMAND + ${CMAKE_COMMAND} -E make_directory "${_TO_TOOL_DIR}" + COMMAND + ${CMAKE_COMMAND} -E create_symlink + "$" + "${_TO_TOOL_PATH}" + VERBATIM + ) +endfunction() + + +#------------------------------------------------------------------------------- +# Tests +#------------------------------------------------------------------------------- + +# cg_add_test_environment_properties +# +# Adds test environment variable properties based on the current build options. +# +function(cg_add_test_environment_properties TEST_NAME) + # COMPILER_GYM_*_DISABLE environment variables may used to skip test cases which + # require both a compiler target backend and compatible runtime HAL driver. + # + # These variables may be set by the test environment, typically as a property + # of some continuous execution test runner or by an individual developer, or + # here by the build system. + # + # Tests which only depend on a compiler target backend or a runtime HAL + # driver, but not both, should generally use a different method of filtering. + if(NOT "${COMPILER_GYM_TARGET_BACKEND_VULKAN-SPIRV}" OR NOT "${COMPILER_GYM_HAL_DRIVER_VULKAN}") + set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "COMPILER_GYM_VULKAN_DISABLE=1") + endif() + if(NOT "${COMPILER_GYM_TARGET_BACKEND_DYLIB-LLVM-AOT}" OR NOT "${COMPILER_GYM_HAL_DRIVER_DYLIB}" + OR NOT "${COMPILER_GYM_HAL_DRIVER_DYLIB_SYNC}") + set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "COMPILER_GYM_LLVMAOT_DISABLE=1") + endif() +endfunction() diff --git a/build_tools/cmake/cg_py_binary.cmake b/build_tools/cmake/cg_py_binary.cmake new file mode 100644 index 0000000000..286bc05121 --- /dev/null +++ b/build_tools/cmake/cg_py_binary.cmake @@ -0,0 +1,45 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_binary.cmake +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(cg_py_library) + +# cg_cc_binary() +# +# CMake function to imitate Bazel's py_binary rule. +# +# Parameters: +# NAME: name of target (see Note) +# SRCS: List of source files for the binary +# GENERATED_SRCS: List of source files for the binary that are generated by other targets +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# TESTONLY: When added, this target will only be built if user passes -DCOMPILER_GYM_BUILD_TESTS=ON to CMake. +# +# Note: +# cg_py_binary will create a binary called ${PACKAGE_NAME}_${NAME}, e.g. +# cg_base_foo with two alias (readonly) targets, a qualified +# ${PACKAGE_NS}::${NAME} and an unqualified ${NAME}. Thus NAME must be globally +# unique in the project. +# +function(cg_py_binary) + cmake_parse_arguments( + _RULE + "PUBLIC;TESTONLY" + "NAME;SRCS;GENERATED_SRCS" + "DATA;DEPS" + ${ARGN} + ) + + # Currently the same as adding a library. + # When install rules are added they will need to split. + cg_py_library(${ARGV}) +endfunction() diff --git a/build_tools/cmake/cg_py_library.cmake b/build_tools/cmake/cg_py_library.cmake new file mode 100644 index 0000000000..3847a065cd --- /dev/null +++ b/build_tools/cmake/cg_py_library.cmake @@ -0,0 +1,88 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_library.cmake +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include_guard(GLOBAL) + +include(CMakeParseArguments) +include(cg_macros) + +# cg_py_library() +# +# CMake function to imitate Bazel's py_library rule. +# +# Parameters: +# NAME: name of target (see Note) +# SRCS: List of source files for the library +# GENERATED_SRCS: List of source files for the library that are generated by other targets +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# TESTONLY: When added, this target will only be built if user passes -DCOMPILER_GYM_BUILD_TESTS=ON to CMake. +# +function(cg_py_library) + cmake_parse_arguments( + _RULE + "PUBLIC;TESTONLY" + "NAME" + "SRCS;GENERATED_SRCS;DATA;DEPS" + ${ARGN} + ) + + if(_RULE_TESTONLY AND NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + # TODO(boian): remove this renaming when call sites do not include ":" in target dependency names + rename_bazel_targets(_RULE_DEPS "${_RULE_DEPS}") + + # Prefix the library with the package name, so we get: cg_package_name. + rename_bazel_targets(_NAME "${_RULE_NAME}") + + unset(_BIN_PATHS) + # Symlink each file as its own target. + foreach(_SRC_FILE ${_RULE_SRCS}) + if(IS_ABSOLUTE _SRC_FILE) + message(FATAL_ERROR "Absolute path for SRCS not allowed.") + endif() + + # _SRC_FILE could have other path components in it, so we need to make a + # directory for it. Ninja does this automatically, but make doesn't. See + # https://github.com/google/iree/issues/6801 + set(_SRC_BIN_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_SRC_FILE}") + get_filename_component(_SRC_BIN_DIR "${_SRC_BIN_PATH}" DIRECTORY) + add_custom_command( + OUTPUT "${_SRC_BIN_PATH}" + COMMAND + ${CMAKE_COMMAND} -E make_directory "${_SRC_BIN_DIR}" + COMMAND ${CMAKE_COMMAND} -E create_symlink + "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}" "${_SRC_BIN_PATH}" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}" + VERBATIM + ) + list(APPEND _BIN_PATHS "${_SRC_BIN_PATH}") + endforeach() + + list(APPEND _BIN_PATHS ${_RULE_GENERATED_SRCS}) + + set(_DEPS ${_RULE_DEPS} ${_BIN_PATHS}) + add_custom_target(${_NAME} ALL DEPENDS ${_DEPS}) + + cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA}) + + # If only one src file set the LOCATION target property to point to it. + list(LENGTH _BIN_PATHS _BIN_PATHS_LENGTH) + if(_BIN_PATHS_LENGTH EQUAL "1") + set_target_properties(${_NAME} PROPERTIES LOCATION "${_BIN_PATHS}") + endif() + + # TODO(boian): add install rules + +endfunction() diff --git a/build_tools/cmake/cg_py_test.cmake b/build_tools/cmake/cg_py_test.cmake new file mode 100644 index 0000000000..588304e022 --- /dev/null +++ b/build_tools/cmake/cg_py_test.cmake @@ -0,0 +1,87 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_test.cmake +# Copyright 2019 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(CMakeParseArguments) +include(cg_installed_test) + +# cg_py_test() +# +# CMake function to imitate Bazel's cc_test rule. +# +# Parameters: +# NAME: name of target. +# SRCS: List of source files +# DATA: List of other targets and files required for this binary +# DEPS: List of other libraries to be linked in to the binary targets +# LABELS: Additional labels to apply to the test. The package path is added +# automatically. +# ARGS command line arguments for the test. +# +# Note: +# cg_cc_test will create a binary called ${PACKAGE_NAME}_${NAME}, e.g. +# cg_base_foo_test. +# +function(cg_py_test) + if(NOT COMPILER_GYM_BUILD_TESTS) + return() + endif() + + cmake_parse_arguments( + _RULE + "" + "NAME;SRCS" + "ARGS;LABELS;DATA;DEPS" + ${ARGN} + ) + + cg_py_binary( + NAME ${_RULE_NAME} + SRCS ${_RULE_SRCS} + DEPS ${_RULE_DEPS} + DATA ${_RULE_DATA} + ) + + rename_bazel_targets(_NAME "${_RULE_NAME}") + cg_package_ns(_PACKAGE_NS) + string(REPLACE "::" "/" _PACKAGE_PATH ${_PACKAGE_NS}) + set(_TEST_NAME "${_PACKAGE_PATH}/${_RULE_NAME}") + set(_LABELS "${_RULE_LABELS}") + list(APPEND _LABELS "${_PACKAGE_PATH}") + + cg_add_installed_test( + TEST_NAME "${_TEST_NAME}" + LABELS "${_LABELS}" + ENVIRONMENT + "PYTHONPATH=${CMAKE_BINARY_DIR}:$ENV{PYTHONPATH}" + "TEST_WORKSPACE=compiler_gym" + #"COMPILER_GYM_RUNFILES=${CMAKE_CURRENT_BINARY_DIR}" + COMMAND + "${CMAKE_SOURCE_DIR}/build_tools/cmake/run_test.${COMPILER_GYM_HOST_SCRIPT_EXT}" + "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_SRCS}" + ${_RULE_ARGS} + INSTALLED_COMMAND + python + "${_PACKAGE_PATH}/${_RULE_SRCS}" + ) + + #cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA}) + + install(FILES ${_RULE_SRCS} + DESTINATION "tests/${_PACKAGE_PATH}" + COMPONENT Tests + ) + + # TODO(boian): Find out how to add deps to tests. + # CMake seems to not allow build targets to be dependencies for tests. + # One way to achieve this is to make the test execution a target. +endfunction() diff --git a/build_tools/cmake/cg_python.cmake b/build_tools/cmake/cg_python.cmake new file mode 100644 index 0000000000..3cabcc98e5 --- /dev/null +++ b/build_tools/cmake/cg_python.cmake @@ -0,0 +1,207 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copyright 2020 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(CMakeParseArguments) +include(cg_installed_test) + +############################################################################### +# Main user rules +############################################################################### + +# Declares that the current source directory is part of a python package +# that will: +# - Will create an install target install-COMPONENT (global, not package +# scoped) +# - Be installed under python_packages/PACKAGE_NAME +# - Have a local path of MODULE_PATH (i.e. namespace package path) +# - Process a setup.py.in from the current directory (if NOT AUGMENT_EXISTING_PACKAGE) +# - Process a version.py.in from the current directory (if NOT AUGMENT_EXISTING_PACKAGE) +# Will set parent scope variables: +# - PY_INSTALL_COMPONENT: Install component. Echoed back from the argument +# for easier addition after this call. +# - PY_INSTALL_PACKAGES_DIR: The python_packages/PACKAGE_NAME path +# - PY_INSTALL_MODULE_DIR: The path to the module directory under +# INSTALL_PACKAGES_DIR. +# +# Add any built deps to DEPS (you will need to add install actions to them +# after). +# +# Any python files in the source directory will be automatically installed +# (recursive). +# +# Also adds a *-stripped target which strips any binaries that are +# present. +# +# Arguments: +# AUGMENT_EXISTING_PACKAGE: Whether to add install artifacts to an existing +# package. +# COMPONENT: Install component +# PACKAGE_NAME: Name of the Python package in the install directory tree. +# MODULE_PATH: Relative path within the package to the module being installed. +# FILES_MATCHING: Explicit arguments to the install FILES_MATCHING directive. +# (Defaults to "PATTERN *.py") +# DEPS: Dependencies. +function(cg_py_install_package) + cmake_parse_arguments(ARG + "AUGMENT_EXISTING_PACKAGE" + "COMPONENT;PACKAGE_NAME;MODULE_PATH" + "DEPS;ADDL_PACKAGE_FILES;FILES_MATCHING" + ${ARGN}) + set(_install_component ${ARG_COMPONENT}) + set(_install_packages_dir "${CMAKE_INSTALL_PREFIX}/python_packages/${ARG_PACKAGE_NAME}") + set(_install_module_dir "${_install_packages_dir}/${ARG_MODULE_PATH}") + set(_target_name install-${_install_component}) + + if(NOT FILES_MATCHING) + set(_files_matching PATTERN "*.py") + else() + set(_files_matching ${ARG_FILES_MATCHING}) + endif() + + if(NOT ARG_AUGMENT_EXISTING_PACKAGE) + configure_file(setup.py.in setup.py) + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/setup.py + ${ARG_ADDL_PACKAGE_FILES} + COMPONENT ${_install_component} + DESTINATION "${_install_packages_dir}" + ) + configure_file(version.py.in version.py) + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/version.py + COMPONENT ${_install_component} + DESTINATION "${_install_module_dir}" + ) + + set(_component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}") + add_custom_target(${_target_name} + COMMAND "${CMAKE_COMMAND}" + ${_component_option} + -P "${CMAKE_BINARY_DIR}/cg_install.cmake" + USES_TERMINAL) + add_custom_target(${_target_name}-stripped + COMMAND "${CMAKE_COMMAND}" + ${_component_option} + -DCMAKE_INSTALL_DO_STRIP=1 + -P "${CMAKE_BINARY_DIR}/cg_install.cmake" + USES_TERMINAL) + endif() + + # Explicit add dependencies in case if we are just extending a package + # vs adding the targets. + if(ARG_DEPS) + add_dependencies(${_target_name} ${ARG_DEPS}) + add_dependencies(${_target_name}-stripped ${ARG_DEPS}) + endif() + + install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ + COMPONENT ${_install_component} + DESTINATION "${_install_module_dir}" + FILES_MATCHING ${_files_matching} + ) + + set(PY_INSTALL_COMPONENT ${_install_component} PARENT_SCOPE) + set(PY_INSTALL_PACKAGES_DIR "${_install_packages_dir}" PARENT_SCOPE) + set(PY_INSTALL_MODULE_DIR "${_install_module_dir}" PARENT_SCOPE) +endfunction() + +# cg_pyext_module() +# +# Builds a native python module (.so/.dylib/.pyd). +# +# Parameters: +# NAME: name of target +# MODULE_NAME: Base-name of the module. +# SRCS: List of source files for the library +# DEPS: List of other targets the test python libraries require +function(cg_pyext_module) + cmake_parse_arguments(ARG + "" + "NAME;MODULE_NAME;UNIX_LINKER_SCRIPT" + "SRCS;DEPS;COPTS;INCLUDES" + ${ARGN}) + + cg_package_ns(_PACKAGE_NS) + list(TRANSFORM ARG_DEPS REPLACE "^::" "${_PACKAGE_NS}::") + list(TRANSFORM ARG_PYEXT_DEPS REPLACE "^::" "${_PACKAGE_NS}::") + # Prefix the library with the package name, so we get: cg_package_name. + rename_bazel_targets(_NAME "${_RULE_NAME}") + + pybind11_add_module( + ${_NAME} + ${ARG_SRCS} + ) + + # Alias the library so that we can + # refer to this target with the namespaced format. + add_library(${_PACKAGE_NS}::${ARG_NAME} ALIAS ${_NAME}) + + target_link_libraries( + ${_NAME} + PRIVATE ${ARG_DEPS} + ) + + set_target_properties( + ${_NAME} PROPERTIES + OUTPUT_NAME "${ARG_MODULE_NAME}" + ) + + target_include_directories(${_NAME} + PUBLIC + "$" + ) + + # pybind11 requires both RTTI and Exceptions, and it does not know that + # we have disabled them globally, so turn them back on. Since this is + # *the only* place in the codebase where we do this, just inline here. + # Note that this is playing with fire and the extension code is structured + # so as not to cause problems with RTTI cross-module issues. + cg_select_compiler_opts(_RTTI_AND_EXCEPTION_COPTS + CLANG_OR_GCC + "-frtti" + "-fexceptions" + MSVC_OR_CLANG_CL + # Configure exception handling for standard C++ behavior. + # - /EHs enables C++ catch-style exceptions + # - /EHc breaks unwinding across extern C boundaries, dramatically reducing + # unwind table size and associated exception handling overhead as the + # compiler can assume no exception will ever be thrown within any function + # annotated with extern "C". + # https://docs.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model + "/EHsc" + # Configure RTTI generation. + # - /GR - Enable generation of RTTI (default) + # - /GR- - Disables generation of RTTI + # https://docs.microsoft.com/en-us/cpp/build/reference/gr-enable-run-time-type-information?view=msvc-160 + "/GR" + ) + + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD 17) + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) + + target_compile_options( + ${_NAME} PRIVATE + ${ARG_COPTS} + ${COMPILER_GYM_DEFAULT_COPTS} + ${_RTTI_AND_EXCEPTION_COPTS} + ) + + # Link flags. + if(UNIX AND NOT APPLE) # Apple does not support linker scripts. + if(ARG_UNIX_LINKER_SCRIPT) + set_target_properties(${_NAME} PROPERTIES LINK_FLAGS + "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/${ARG_UNIX_LINKER_SCRIPT}") + endif() + endif() +endfunction() diff --git a/build_tools/cmake/grpc.cmake b/build_tools/cmake/grpc.cmake new file mode 100644 index 0000000000..2818e2c0f5 --- /dev/null +++ b/build_tools/cmake/grpc.cmake @@ -0,0 +1,123 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include_guard(GLOBAL) +include(CMakeParseArguments) +include(cg_macros) +include(cg_py_library) +include(protobuf) + +function(get_cc_grpc_proto_out_files _PROTO_FILENAME _RESULT) + set(_PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto\\.bin$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME_WITHOUT_EXT}") + set(${_RESULT} + "${_PROTO_FILENAME_WITHOUT_EXT}.grpc.pb.h" + "${_PROTO_FILENAME_WITHOUT_EXT}.grpc.pb.cc" + PARENT_SCOPE) +endfunction() + +function(cc_grpc_library) + cmake_parse_arguments( + _RULE + "PUBLIC;GRPC_ONLY" + "NAME;SRCS" + "DEPS" + ${ARGN} + ) + + if (NOT _RULE_GRPC_ONLY) + message("GRPC_ONLY=False unsupported.") + endif() + + rename_bazel_targets(_DEPS "${_RULE_DEPS}") + rename_bazel_targets(_NAME "${_RULE_NAME}") + rename_bazel_targets(_SRCS "${_RULE_SRCS}") + + get_target_as_relative_dir(${_NAME} _HEADER_DST_DIR) + set(_HEADER_DST_DIR "${CMAKE_CURRENT_BINARY_DIR}/include/") + get_target_property(_DESCRIPTOR_SET_FILE ${_SRCS} PROTO_DESCRIPTOR_SETS) + + get_target_property(_PROTO_FILE ${_SRCS} PROTO_FILES) + file(RELATIVE_PATH _RELATIVE_PROTO_FILE "${CMAKE_SOURCE_DIR}" "${_PROTO_FILE}") + + get_filename_component(_RELATIVE_PROTO_DIR "${_RELATIVE_PROTO_FILE}" DIRECTORY) + get_filename_component(_SRC_FILENAME "${_DESCRIPTOR_SET_FILE}" NAME) + get_cc_grpc_proto_out_files("${_SRC_FILENAME}" _GRPC_PROTO_FILES) + list(TRANSFORM _GRPC_PROTO_FILES PREPEND "${_HEADER_DST_DIR}/${_RELATIVE_PROTO_DIR}/") + + add_custom_command( + OUTPUT ${_GRPC_PROTO_FILES} + COMMAND ${CMAKE_COMMAND} -E make_directory "${_HEADER_DST_DIR}" + COMMAND "${Protobuf_PROTOC_EXECUTABLE}" + --proto_path "${CMAKE_SOURCE_DIR}" + --descriptor_set_in "${_DESCRIPTOR_SET_FILE}" + --grpc_out "${_HEADER_DST_DIR}" + --plugin "protoc-gen-grpc=${_GRPC_CPP_PLUGIN_EXECUTABLE}" + "${_RELATIVE_PROTO_FILE}" + DEPENDS "${Protobuf_PROTOC_EXECUTABLE}" "${_DESCRIPTOR_SET_FILE}" "${_PROTO_FILE}" ${_DEPS} + VERBATIM) + + cg_cc_library( + NAME ${_RULE_NAME} + SRCS ${_GRPC_PROTO_FILES} + ABS_DEPS grpc++ + INCLUDES "${CMAKE_CURRENT_BINARY_DIR}/include" + PUBLIC + ) +endfunction() + +function(get_py_grpc_proto_out_files _PROTO_FILENAME _RESULT) + set(_PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto\\.bin$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME_WITHOUT_EXT}") + set(${_RESULT} + "${_PROTO_FILENAME_WITHOUT_EXT}_pb2_grpc.py" + PARENT_SCOPE) +endfunction() + +function(py_grpc_library) + cmake_parse_arguments( + _RULE + "" + "NAME;SRCS" + "DEPS" + ${ARGN} + ) + + rename_bazel_targets(_DEPS "${_RULE_DEPS}") + rename_bazel_targets(_SRCS "${_RULE_SRCS}") + + get_target_property(_DESCRIPTOR_SET_FILE ${_SRCS} PROTO_DESCRIPTOR_SETS) + get_filename_component(_SRC_FILENAME "${_DESCRIPTOR_SET_FILE}" NAME) + get_py_grpc_proto_out_files("${_SRC_FILENAME}" _PY_GRPC_PROTO_FILES) + set(_PYTHON_DST_DIR "${CMAKE_CURRENT_BINARY_DIR}") + set(_ABS_PATH_PY_GRPC_PROTO_FILES ${_PY_GRPC_PROTO_FILES}) + list(TRANSFORM _ABS_PATH_PY_GRPC_PROTO_FILES PREPEND "${_PYTHON_DST_DIR}/") + + get_target_property(_PROTO_FILE ${_SRCS} PROTO_FILES) + get_filename_component(_PROTO_FILENAME "${_PROTO_FILE}" NAME) + file(RELATIVE_PATH _RELATIVE_PROTO_FILE "${CMAKE_SOURCE_DIR}" "${_PROTO_FILE}") + + add_custom_command( + OUTPUT ${_ABS_PATH_PY_GRPC_PROTO_FILES} + COMMAND ${CMAKE_COMMAND} -E make_directory "${_PYTHON_DST_DIR}" + COMMAND "${Python3_EXECUTABLE}" + -m grpc_tools.protoc + --proto_path "${CMAKE_SOURCE_DIR}" + --descriptor_set_in "${_DESCRIPTOR_SET_FILE}" + --grpc_python_out "${CMAKE_BINARY_DIR}" + "${_RELATIVE_PROTO_FILE}" + DEPENDS "${Python3_EXECUTABLE}" "${_DESCRIPTOR_SET_FILE}" "${_PROTO_FILE}" ${_DEPS} + VERBATIM) + + cg_py_library( + NAME "${_RULE_NAME}" + GENERATED_SRCS ${_PY_GRPC_PROTO_FILES} + ) +endfunction() diff --git a/build_tools/cmake/protobuf.cmake b/build_tools/cmake/protobuf.cmake new file mode 100644 index 0000000000..ce45263036 --- /dev/null +++ b/build_tools/cmake/protobuf.cmake @@ -0,0 +1,154 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include_guard(GLOBAL) +include(CMakeParseArguments) +include(cg_macros) +include(cg_py_library) + +function(proto_library) + cmake_parse_arguments( + _RULE + "PUBLIC" + "NAME;SRCS" + "DEPS" + ${ARGN} + ) + + rename_bazel_targets(_RULE_DEPS "${_RULE_DEPS}") + rename_bazel_targets(_RULE_NAME "${_RULE_NAME}") + + set(_SRC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_SRCS}") + set(_DST_FILE "${CMAKE_CURRENT_BINARY_DIR}/${_RULE_SRCS}.bin") + get_filename_component(_DST_DIR "${_DST_FILE}" DIRECTORY) + file(RELATIVE_PATH _RELATIVE_PROTO_FILE "${CMAKE_SOURCE_DIR}" "${_SRC_FILE}") + + add_custom_command( + OUTPUT "${_DST_FILE}" + COMMAND ${CMAKE_COMMAND} -E make_directory "${_DST_DIR}" + COMMAND "${Protobuf_PROTOC_EXECUTABLE}" + --proto_path "${CMAKE_SOURCE_DIR}" + --descriptor_set_out "${_DST_FILE}" + "${_RELATIVE_PROTO_FILE}" + DEPENDS "${Protobuf_PROTOC_EXECUTABLE}" "${_SRC_FILE}" ${_RULE_DEPS} + VERBATIM) + + add_custom_target(${_RULE_NAME} ALL DEPENDS "${_DST_FILE}") + set_target_properties(${_RULE_NAME} PROPERTIES PROTO_DESCRIPTOR_SETS "${_DST_FILE}") + set_target_properties(${_RULE_NAME} PROPERTIES PROTO_FILES "${_SRC_FILE}") +endfunction() + +function(get_cc_proto_out_files _PROTO_FILENAME _RESULT) + set(_PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto\\.bin$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME_WITHOUT_EXT}") + set(${_RESULT} + "${_PROTO_FILENAME_WITHOUT_EXT}.pb.h" + "${_PROTO_FILENAME_WITHOUT_EXT}.pb.cc" + PARENT_SCOPE) +endfunction() + +function(cc_proto_library) + cmake_parse_arguments( + _RULE + "PUBLIC" + "NAME;DEPS" + "" + ${ARGN} + ) + + rename_bazel_targets(_DEPS "${_RULE_DEPS}") + rename_bazel_targets(_NAME "${_RULE_NAME}") + + get_target_as_relative_dir(${_NAME} _HEADER_DST_DIR) + set(_HEADER_DST_DIR "${CMAKE_CURRENT_BINARY_DIR}/include") + get_target_property(_DESCRIPTOR_SET_FILE ${_DEPS} PROTO_DESCRIPTOR_SETS) + + get_target_property(_PROTO_FILE ${_DEPS} PROTO_FILES) + get_filename_component(_PROTO_FILENAME "${_PROTO_FILE}" NAME) + file(RELATIVE_PATH _RELATIVE_PROTO_FILE "${CMAKE_SOURCE_DIR}" "${_PROTO_FILE}") + + get_filename_component(_RELATIVE_PROTO_DIR "${_RELATIVE_PROTO_FILE}" DIRECTORY) + get_filename_component(_SRC_FILENAME "${_DESCRIPTOR_SET_FILE}" NAME) + get_cc_proto_out_files("${_SRC_FILENAME}" _CC_PROTO_FILES) + list(TRANSFORM _CC_PROTO_FILES PREPEND "${_HEADER_DST_DIR}/${_RELATIVE_PROTO_DIR}/") + + add_custom_command( + OUTPUT ${_CC_PROTO_FILES} + COMMAND ${CMAKE_COMMAND} -E make_directory "${_HEADER_DST_DIR}" + COMMAND "${Protobuf_PROTOC_EXECUTABLE}" + --proto_path "${CMAKE_SOURCE_DIR}" + --descriptor_set_in "${_DESCRIPTOR_SET_FILE}" + --cpp_out "${_HEADER_DST_DIR}" + "${_RELATIVE_PROTO_FILE}" + DEPENDS + "${Protobuf_PROTOC_EXECUTABLE}" + "${_DESCRIPTOR_SET_FILE}" + "${_PROTO_FILE}" + ${_DEPS} + VERBATIM) + + cg_cc_library( + NAME ${_RULE_NAME} + SRCS ${_CC_PROTO_FILES} + ABS_DEPS protobuf::libprotobuf + INCLUDES "${CMAKE_CURRENT_BINARY_DIR}/include" + PUBLIC + ) +endfunction() + +function(get_py_proto_out_files _PROTO_FILENAME _RESULT) + set(_PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME}") + string(REGEX REPLACE "\\.proto\\.bin$" "" _PROTO_FILENAME_WITHOUT_EXT "${_PROTO_FILENAME_WITHOUT_EXT}") + set(${_RESULT} + "${_PROTO_FILENAME_WITHOUT_EXT}_pb2.py" + PARENT_SCOPE) +endfunction() + +function(py_proto_library) + cmake_parse_arguments( + _RULE + "PUBLIC" + "NAME;DEPS" + "" + ${ARGN} + ) + + rename_bazel_targets(_DEPS "${_RULE_DEPS}") + + get_target_property(_DESCRIPTOR_SET_FILE ${_DEPS} PROTO_DESCRIPTOR_SETS) + get_filename_component(_SRC_FILENAME "${_DESCRIPTOR_SET_FILE}" NAME) + get_py_proto_out_files("${_SRC_FILENAME}" _PY_PROTO_FILES) + set(_PYTHON_DST_DIR "${CMAKE_CURRENT_BINARY_DIR}") + set(_ABS_PATH_PY_PROTO_FILES ${_PY_PROTO_FILES}) + list(TRANSFORM _ABS_PATH_PY_PROTO_FILES PREPEND "${_PYTHON_DST_DIR}/") + + get_target_property(_PROTO_FILE ${_DEPS} PROTO_FILES) + file(RELATIVE_PATH _RELATIVE_PROTO_FILE "${CMAKE_SOURCE_DIR}" "${_PROTO_FILE}") + + add_custom_command( + OUTPUT ${_ABS_PATH_PY_PROTO_FILES} + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}" + COMMAND "${Protobuf_PROTOC_EXECUTABLE}" + --proto_path "${CMAKE_SOURCE_DIR}" + --descriptor_set_in "${_DESCRIPTOR_SET_FILE}" + --python_out "${CMAKE_BINARY_DIR}" + "${_RELATIVE_PROTO_FILE}" + DEPENDS + "${Protobuf_PROTOC_EXECUTABLE}" + "${_DESCRIPTOR_SET_FILE}" + "${_PROTO_FILE}" + ${_DEPS} + VERBATIM) + + cg_py_library( + NAME "${_RULE_NAME}" + GENERATED_SRCS ${_PY_PROTO_FILES} + ) +endfunction() diff --git a/build_tools/cmake/run_test.sh b/build_tools/cmake/run_test.sh new file mode 100755 index 0000000000..686114d878 --- /dev/null +++ b/build_tools/cmake/run_test.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# Copyright 2020 The IREE Authors +# +# Licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# A wrapper around a test command that performs setup and teardown. This is +# appranetly not supported natively in ctest/cmake. + +set -x +set -e + +function cleanup() { + echo "Cleaning up test environment" + rm -rf ${TEST_TMPDIR?} +} + +echo "Creating test environment" +rm -rf "${TEST_TMPDIR?}" # In case this wasn't cleaned up previously +mkdir -p "${TEST_TMPDIR?}" +trap cleanup EXIT +# Execute whatever we were passed. +"$@" diff --git a/build_tools/cmake/set_command_pythonpath.cmake b/build_tools/cmake/set_command_pythonpath.cmake new file mode 100644 index 0000000000..96a9c2a567 --- /dev/null +++ b/build_tools/cmake/set_command_pythonpath.cmake @@ -0,0 +1,23 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include(CMakeParseArguments) + +function(set_command_pythonpath) + cmake_parse_arguments( + _ARG + "" + "COMMAND;RESULT" + "" + ${ARGN} + ) + + if(COMPILER_GYM_PYTHONPATH) + set(${_ARG_RESULT} "\"${CMAKE_COMMAND}\" -E env \"PYTHONPATH=${COMPILER_GYM_PYTHONPATH}\" ${_ARG_COMMAND}" PARENT_SCOPE) + else() + set(${_ARG_RESULT} ${_ARG_COMMAND} PARENT_SCOPE) + endif() + +endfunction() diff --git a/build_tools/cmake/write_cache_script.cmake b/build_tools/cmake/write_cache_script.cmake new file mode 100644 index 0000000000..4d452d8c8a --- /dev/null +++ b/build_tools/cmake/write_cache_script.cmake @@ -0,0 +1,36 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include_guard(GLOBAL) + +function(write_cache_script _DST_FILE) + file(WRITE "${_DST_FILE}" "") + set(_VARS + CMAKE_BUILD_TYPE + CMAKE_GENERATOR + CMAKE_C_COMPILER + CMAKE_CXX_COMPILER + CMAKE_CXX_STANDARD + CMAKE_CXX_FLAGS + CMAKE_C_FLAGS + CMAKE_GENERATOR_TOOLSET + CMAKE_GENERATOR_PLATFORM + CMAKE_C_COMPILER_LAUNCHER + CMAKE_CXX_COMPILER_LAUNCHER + CMAKE_MODULE_LINKER_FLAGS_INIT + CMAKE_MODULE_LINKER_FLAGS + CMAKE_STATIC_LINKER_FLAGS_INIT + CMAKE_STATIC_LINKER_FLAGS + CMAKE_SHARED_LINKER_FLAGS_INIT + CMAKE_SHARED_LINKER_FLAGS + CMAKE_EXE_LINKER_FLAGS_INIT + CMAKE_EXE_LINKER_FLAGS + ) + foreach(_VAR in ${_VARS}) + if(DEFINED ${_VAR}) + file(APPEND "${_DST_FILE}" "set(${_VAR} \"${${_VAR}}\" CACHE STRING \"\")\n") + endif() + endforeach() +endfunction() diff --git a/compiler_gym/CMakeLists.txt b/compiler_gym/CMakeLists.txt new file mode 100644 index 0000000000..402c6dd1cf --- /dev/null +++ b/compiler_gym/CMakeLists.txt @@ -0,0 +1,114 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +# This target trickery with compiler_gym and compiler_gym_partial +# is needed because specs.py imports the compiler_gym module, +# therefore creating a circular dependency. +# compiler_gym_partial is all the other bits of the proto package so that +# specs.py can import it. +cg_py_library( + NAME + compiler_gym + GENERATED_SRCS + "$" + DEPS + ::compiler_gym_partial + compiler_gym::envs::llvm::specs + PUBLIC +) + +cg_py_library( + NAME + compiler_gym_partial + SRCS + "__init__.py" + DEPS + ::random_replay + ::random_search + ::validate + compiler_gym::bin::bin + compiler_gym::datasets::datasets + compiler_gym::envs::envs + compiler_gym::leaderboard::leaderboard + compiler_gym::service::service + compiler_gym::spaces::spaces + compiler_gym::util::util + compiler_gym::util::flags::flags + compiler_gym::wrappers::wrappers + PUBLIC +) + +cg_py_library( + NAME + compiler_env_state + SRCS + "compiler_env_state.py" + DEPS + compiler_gym::datasets::uri + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + random_replay + SRCS + "random_replay.py" + DEPS + ::random_search + compiler_gym::envs::envs + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + random_search + SRCS + "random_search.py" + DATA + compiler_gym::envs::llvm::service::service + DEPS + compiler_gym::envs::envs + compiler_gym::service::connection + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + validate + SRCS + "validate.py" + DEPS + ::validation_error + ::validation_result + compiler_gym::envs::compiler_env + compiler_gym::spaces::spaces + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + validation_error + SRCS + "validation_error.py" + PUBLIC +) + +cg_py_library( + NAME + validation_result + SRCS + "validation_result.py" + DEPS + ::compiler_env_state + ::validation_error + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/bin/CMakeLists.txt b/compiler_gym/bin/CMakeLists.txt new file mode 100644 index 0000000000..98c437f733 --- /dev/null +++ b/compiler_gym/bin/CMakeLists.txt @@ -0,0 +1,99 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + bin + DEPS + ::datasets + ::manual_env + ::random_replay + ::random_search + ::service + ::validate + PUBLIC +) + +cg_py_binary( + NAME + datasets + SRCS + "datasets.py" + DEPS + ::service + compiler_gym::datasets::datasets + compiler_gym::envs::envs + compiler_gym::util::util + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + manual_env + SRCS + "manual_env.py" + DEPS + compiler_gym::envs::envs + compiler_gym::util::util + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + random_eval + SRCS + "random_eval.py" + DEPS + compiler_gym::random_search + compiler_gym::util::util + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + random_search + SRCS + "random_search.py" + DEPS + compiler_gym::random_search + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + random_replay + SRCS + "random_replay.py" + DEPS + compiler_gym::random_search + compiler_gym::util::util + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + service + SRCS + "service.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::envs + compiler_gym::spaces::spaces + compiler_gym::util::util + compiler_gym::util::flags::flags +) + +cg_py_binary( + NAME + validate + SRCS + "validate.py" + DEPS + compiler_gym::util::util + compiler_gym::util::flags::flags + compiler_gym::validate +) diff --git a/compiler_gym/datasets/CMakeLists.txt b/compiler_gym/datasets/CMakeLists.txt new file mode 100644 index 0000000000..2191dc664b --- /dev/null +++ b/compiler_gym/datasets/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + datasets + SRCS + "__init__.py" + "benchmark.py" + "dataset.py" + "datasets.py" + "files_dataset.py" + "tar_dataset.py" + DEPS + ::uri + compiler_gym::service::proto::proto + compiler_gym::util::util + compiler_gym::validation_result + PUBLIC +) + +cg_py_library( + NAME + uri + SRCS + "uri.py" + PUBLIC +) diff --git a/compiler_gym/envs/CMakeLists.txt b/compiler_gym/envs/CMakeLists.txt new file mode 100644 index 0000000000..15a1f64cab --- /dev/null +++ b/compiler_gym/envs/CMakeLists.txt @@ -0,0 +1,36 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + envs + SRCS + "__init__.py" + DEPS + ::compiler_env + compiler_gym::envs::gcc::gcc + compiler_gym::envs::llvm::llvm + compiler_gym::envs::loop_tool::loop_tool + PUBLIC +) + +cg_py_library( + NAME + compiler_env + SRCS + "compiler_env.py" + DEPS + compiler_gym::compiler_env_state + compiler_gym::datasets::datasets + compiler_gym::service::service + compiler_gym::service::proto::proto + compiler_gym::spaces::spaces + compiler_gym::util::util + compiler_gym::validation_result + compiler_gym::views::views + PUBLIC +) diff --git a/compiler_gym/envs/gcc/CMakeLists.txt b/compiler_gym/envs/gcc/CMakeLists.txt new file mode 100644 index 0000000000..301315d1b6 --- /dev/null +++ b/compiler_gym/envs/gcc/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + gcc + SRCS + "__init__.py" + "gcc.py" + "gcc_env.py" + "gcc_rewards.py" + DATA + compiler_gym::envs::gcc::service::service + DEPS + compiler_gym::envs::compiler_env + compiler_gym::envs::gcc::datasets::datasets + compiler_gym::service::service + compiler_gym::service::runtime::runtime + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/envs/gcc/datasets/CMakeLists.txt b/compiler_gym/envs/gcc/datasets/CMakeLists.txt new file mode 100644 index 0000000000..4ea3d20706 --- /dev/null +++ b/compiler_gym/envs/gcc/datasets/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + datasets + SRCS + "__init__.py" + "anghabench.py" + "chstone.py" + "csmith.py" + DATA + compiler_gym::third_party::csmith::all + DEPS + compiler_gym::datasets::datasets + compiler_gym::service::proto::proto + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/envs/gcc/service/CMakeLists.txt b/compiler_gym/envs/gcc/service/CMakeLists.txt new file mode 100644 index 0000000000..b94bf8f55e --- /dev/null +++ b/compiler_gym/envs/gcc/service/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_filegroup( + NAME "service" + FILES + "${CMAKE_CURRENT_LIST_DIR}/gcc_service.py" + "${CMAKE_CURRENT_LIST_DIR}/compiler_gym-gcc-service" +) diff --git a/compiler_gym/envs/llvm/CMakeLists.txt b/compiler_gym/envs/llvm/CMakeLists.txt new file mode 100644 index 0000000000..84bd9b75b0 --- /dev/null +++ b/compiler_gym/envs/llvm/CMakeLists.txt @@ -0,0 +1,101 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + llvm + SRCS + "__init__.py" + DATA + compiler_gym::envs::llvm::service::service + DEPS + ::compute_observation + ::llvm_benchmark + ::llvm_env + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME compute_observation + SRCS compute_observation.py + DATA compiler_gym::envs::llvm::service::compute_observation-files + DEPS compiler_gym::util::util +) + +cg_py_library( + NAME + llvm_benchmark + SRCS + "llvm_benchmark.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::service::proto::proto + compiler_gym::third_party::llvm::llvm + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + llvm_env + SRCS + "llvm_env.py" + DEPS + ::llvm_benchmark + ::llvm_rewards + compiler_gym::datasets::datasets + compiler_gym::envs::compiler_env + compiler_gym::envs::llvm::datasets::datasets + compiler_gym::spaces::spaces + compiler_gym::third_party::autophase::autophase + compiler_gym::third_party::inst2vec::inst2vec + compiler_gym::third_party::llvm::llvm + compiler_gym::third_party::llvm::instcount + PUBLIC +) + +cg_py_library( + NAME + llvm_rewards + SRCS + "llvm_rewards.py" + DEPS + compiler_gym::service::service + compiler_gym::spaces::spaces + compiler_gym::util::util + compiler_gym::views::views + PUBLIC +) + +string(CONCAT _CMD + "\"${Python3_EXECUTABLE}\" " + "\"$\" " + "\"$\" " + "\"$@\"") +set_command_pythonpath(COMMAND "${_CMD}" RESULT _CMD) +cg_genrule( + NAME specs + OUTS "specs.py" + COMMAND "${_CMD}" + DEPENDS + ::make_specs + compiler_gym::compiler_gym_partial + compiler_gym::envs::llvm::service::service + compiler_gym::envs::llvm::service::compiler_gym-llvm-service +) + +cg_py_binary( + NAME + make_specs + SRCS + "make_specs.py" + DATA "${CMAKE_CURRENT_BINARY_DIR}/service/passes/flag_descriptions.txt" + DEPS + ::llvm_env + compiler_gym::util::util +) diff --git a/compiler_gym/envs/llvm/__init__.py b/compiler_gym/envs/llvm/__init__.py index 0f7475ffd9..ebbeaff004 100644 --- a/compiler_gym/envs/llvm/__init__.py +++ b/compiler_gym/envs/llvm/__init__.py @@ -3,6 +3,7 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """Register the LLVM environments.""" +import sys from itertools import product from compiler_gym.envs.llvm.compute_observation import compute_observation @@ -12,7 +13,11 @@ make_benchmark, ) from compiler_gym.envs.llvm.llvm_env import LlvmEnv -from compiler_gym.envs.llvm.specs import observation_spaces, reward_spaces + +# TODO(github.com/facebookresearch/CompilerGym/issues/506): Tidy up. +if "compiler_gym.envs.llvm.is_making_specs" not in sys.modules: + from compiler_gym.envs.llvm.specs import observation_spaces, reward_spaces + from compiler_gym.util.registration import register from compiler_gym.util.runfiles_path import runfiles_path diff --git a/compiler_gym/envs/llvm/datasets/CMakeLists.txt b/compiler_gym/envs/llvm/datasets/CMakeLists.txt new file mode 100644 index 0000000000..c20fe166db --- /dev/null +++ b/compiler_gym/envs/llvm/datasets/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + datasets + SRCS + "__init__.py" + "anghabench.py" + "cbench.py" + "chstone.py" + "clgen.py" + "csmith.py" + "llvm_stress.py" + "poj104.py" + DATA + compiler_gym::third_party::csmith::all + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::llvm::llvm_benchmark + compiler_gym::service::proto::proto + compiler_gym::third_party::llvm::llvm + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/envs/llvm/make_specs.py b/compiler_gym/envs/llvm/make_specs.py index c44ce0d3d8..76f53dcf9f 100644 --- a/compiler_gym/envs/llvm/make_specs.py +++ b/compiler_gym/envs/llvm/make_specs.py @@ -11,10 +11,18 @@ # TODO: As we add support for more compilers we could generalize this script # to work with other compiler services rather than hardcoding to LLVM. import sys +import types from pathlib import Path -from compiler_gym.envs.llvm.llvm_env import LlvmEnv -from compiler_gym.util.runfiles_path import runfiles_path +# TODO(github.com/facebookresearch/CompilerGym/issues/506): Avoids circular +# dependency during specs.py generation, because it is imported from +# compiler_gym.envs.llvm before being generated. +sys.modules["compiler_gym.envs.llvm.is_making_specs"] = types.ModuleType( + "compiler_gym.envs.llvm.is_making_specs" +) + +from compiler_gym.envs.llvm.llvm_env import LlvmEnv # noqa: E402 +from compiler_gym.util.runfiles_path import runfiles_path # noqa: E402 with open( runfiles_path("compiler_gym/envs/llvm/service/passes/flag_descriptions.txt") diff --git a/compiler_gym/envs/llvm/service/CMakeLists.txt b/compiler_gym/envs/llvm/service/CMakeLists.txt new file mode 100644 index 0000000000..3fdd67f9d7 --- /dev/null +++ b/compiler_gym/envs/llvm/service/CMakeLists.txt @@ -0,0 +1,274 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +set(_DEPS "compiler_gym-llvm-service") +if(DARWIN) + list(APPEND _DEPS ::libLLVMPolly) + #TODO(boian): figure out what is this target. + #list(APPEND _DEPS "@llvm//:darwin") +endif() +cg_filegroup( + NAME "service" + DEPENDS ${_DEPS} +) + +cg_genrule( + NAME libLLVMPolly + OUTS "libLLVMPolly.so" + COMMAND + "cp $ $@" + ABS_DEPENDS + LLVMPolly +) + +cg_cc_binary( + NAME + compiler_gym-llvm-service + SRCS + "RunService.cc" + DEPS + ::LlvmSession + compiler_gym::service::runtime::cc_runtime +) + +cg_cc_library( + NAME + ActionSpace + HDRS + "ActionSpace.h" + "$/ActionEnum.h" + SRCS + "ActionSpace.cc" + DEPS + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::util::EnumUtil + compiler_gym::util::Unreachable + ABS_DEPS + fmt + magic_enum + NON_LIB_DEPS + compiler_gym::envs::llvm::service::passes::actions_genfiles + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS core support bitwriter) +cg_cc_library( + NAME + Benchmark + HDRS + "Benchmark.h" + SRCS + "Benchmark.cc" + DEPS + ::Cost + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::util::GrpcStatusMacros + compiler_gym::util::RunfilesPath + compiler_gym::util::Subprocess + ABS_DEPS + Boost::filesystem + grpc++ + fmt + glog::glog + ${_LLVM_LIBS} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS core) +cg_cc_library( + NAME + BenchmarkFactory + HDRS + "BenchmarkFactory.h" + SRCS + "BenchmarkFactory.cc" + DEPS + ::Benchmark + ::Cost + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::util::GrpcStatusMacros + compiler_gym::util::RunfilesPath + compiler_gym::util::StrLenConstexpr + ABS_DEPS + Boost::filesystem + grpc++ + fmt + glog::glog + ${_LLVM_LIBS} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS core support irreader) +cg_cc_binary( + NAME compute_observation + SRCS ComputeObservation.cc + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + DEPS + ::BenchmarkFactory + ::Observation + ::ObservationSpaces + compiler_gym::service::proto::compiler_gym_service_cc + ABS_DEPS + Boost::filesystem + glog::glog + ${_LLVM_LIBS} + magic_enum + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} +) + +set(_FILES "${CMAKE_CURRENT_BINARY_DIR}/compute_observation") +if(DARWIN) + message(FATAL_ERROR "TODO(boian): implement") +endif() +cg_filegroup( + NAME compute_observation-files + FILES ${_FILES} +) + +llvm_map_components_to_libnames(_LLVM_LIBS core transformutils ipo) +cg_cc_library( + NAME + Cost + HDRS + "Cost.h" + SRCS + "Cost.cc" + DEPS + compiler_gym::util::GrpcStatusMacros + compiler_gym::util::RunfilesPath + compiler_gym::util::Subprocess + compiler_gym::util::Unreachable + ABS_DEPS + Boost::filesystem + Boost::headers + grpc++ + fmt + glog::glog + ${_LLVM_LIBS} + magic_enum + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS + core analysis coroutines objcarcopts target codegen + x86codegen x86asmparser #TODO(boian): can these be found programmatically + ) +cg_cc_library( + NAME + LlvmSession + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + HDRS + "$/ActionHeaders.h" + "$/ActionSwitch.h" + "LlvmSession.h" + SRCS + "LlvmSession.cc" + DEPS + ::ActionSpace + ::Benchmark + ::BenchmarkFactory + ::Cost + ::Observation + ::ObservationSpaces + compiler_gym::service::CompilationSession + compiler_gym::service::proto::compiler_gym_service_cc_grpc + compiler_gym::third_party::autophase::InstCount + compiler_gym::util::EnumUtil + compiler_gym::util::GrpcStatusMacros + compiler_gym::util::RunfilesPath + ABS_DEPS + Boost::filesystem + Boost::headers + fmt + glog::glog + ${_LLVM_LIBS} + magic_enum + nlohmann_json::nlohmann_json + ProGraML::graph::format::node_link_graph + ProGraML::ir::llvm::llvm-10 + ProGraML::proto::programl_cc + Subprocess::libsubprocess + CpuInfo::cpuinfo + Clog::libclog + INCLUDES + ${LLVM_INCLUDE_DIRS} + "$" + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS + core support bitwriter + ) +cg_cc_library( + NAME Observation + SRCS Observation.cc + HDRS Observation.h + DEPS + ::Benchmark + ::Cost + ::ObservationSpaces + compiler_gym::service::proto::compiler_gym_service_cc_grpc + compiler_gym::third_party::autophase::InstCount + compiler_gym::util::GrpcStatusMacros + ABS_DEPS + CpuInfo::cpuinfo + Boost::filesystem + glog::glog + ${_LLVM_LIBS} + magic_enum + nlohmann_json::nlohmann_json + ProGraML::graph::format::node_link_graph + ProGraML::ir::llvm::llvm-10 + ProGraML::proto::programl_cc + Clog::libclog + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} +) + +cg_cc_library( + NAME + ObservationSpaces + HDRS + "ObservationSpaces.h" + SRCS + "ObservationSpaces.cc" + DEPS + ::Benchmark + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::third_party::llvm::InstCount + compiler_gym::util::EnumUtil + ABS_DEPS + glog::glog + magic_enum + nlohmann_json::nlohmann_json + ProGraML::graph::format::node_link_graph + ProGraML::proto::programl_cc + PUBLIC +) diff --git a/compiler_gym/envs/llvm/service/passes/CMakeLists.txt b/compiler_gym/envs/llvm/service/passes/CMakeLists.txt new file mode 100644 index 0000000000..fde84228cd --- /dev/null +++ b/compiler_gym/envs/llvm/service/passes/CMakeLists.txt @@ -0,0 +1,102 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +string(CONCAT _CMD + "\"${Python3_EXECUTABLE}\" " + "\"$\"" + " \"${LLVM_SRC_DIR}/llvm\" > $@") +set_command_pythonpath(COMMAND "${_CMD}" RESULT _CMD) +cg_genrule( + NAME passes_list + OUTS "passes_list.csv" + COMMAND ${_CMD} + DEPENDS + ::extract_passes_from_llvm_source_tree +) + +string(CONCAT _CMD + "\"${Python3_EXECUTABLE}\" " + "\"$\" < " + " \"$\" > $@") +set_command_pythonpath(COMMAND "${_CMD}" RESULT _CMD) +cg_genrule( + NAME actions_csv + OUTS "actions.csv" + COMMAND ${_CMD} + DEPENDS + ::passes_list + ::filter_action_space +) + +cg_py_library( + NAME + common + SRCS + "common.py" + PUBLIC +) + +cg_py_library( + NAME + config + SRCS + "config.py" + DEPS + ::common + PUBLIC +) + +cg_py_binary( + NAME + extract_passes_from_llvm_source_tree + SRCS + "extract_passes_from_llvm_source_tree.py" + DEPS + ::common + ::config +) + +cg_py_binary( + NAME + make_action_space_genfiles + SRCS + "make_action_space_genfiles.py" + DEPS + ::common + ::config +) + +cg_py_binary( + NAME + filter_action_space + SRCS + "filter_action_space.py" + DEPS + ::common + ::config +) + +string(CONCAT _CMD + "\"${Python3_EXECUTABLE}\" " + "\"$\"" + " $(@D) < \"$\"" +) +set_command_pythonpath(COMMAND "${_CMD}" RESULT _CMD) +cg_genrule( + NAME actions_genfiles + OUTS + "ActionEnum.h" + "ActionSwitch.h" + "ActionHeaders.h" + "flags.txt" + "flag_descriptions.txt" + COMMAND + ${_CMD} + DEPENDS + ::actions_csv + ::make_action_space_genfiles +) diff --git a/compiler_gym/envs/loop_tool/CMakeLists.txt b/compiler_gym/envs/loop_tool/CMakeLists.txt new file mode 100644 index 0000000000..1b707400fe --- /dev/null +++ b/compiler_gym/envs/loop_tool/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME loop_tool + SRCS + "__init__.py" + "loop_tool_env.py" + DATA compiler_gym::envs::loop_tool::service::service + DEPS + compiler_gym::envs::compiler_env + compiler_gym::service::service + compiler_gym::service::proto::proto + compiler_gym::service::runtime::runtime + PUBLIC +) diff --git a/compiler_gym/envs/loop_tool/service/CMakeLists.txt b/compiler_gym/envs/loop_tool/service/CMakeLists.txt new file mode 100644 index 0000000000..aba317c951 --- /dev/null +++ b/compiler_gym/envs/loop_tool/service/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_filegroup( + NAME service + FILES + "${CMAKE_CURRENT_LIST_DIR}/compiler_gym-loop_tool-service" + "${CMAKE_CURRENT_LIST_DIR}/loop_tool_compilation_session.py" +) diff --git a/compiler_gym/leaderboard/CMakeLists.txt b/compiler_gym/leaderboard/CMakeLists.txt new file mode 100644 index 0000000000..9c95813e3a --- /dev/null +++ b/compiler_gym/leaderboard/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + leaderboard + SRCS + "__init__.py" + DEPS + ::llvm_instcount + PUBLIC +) + +cg_py_library( + NAME + llvm_instcount + SRCS + "llvm_instcount.py" + DEPS + compiler_gym::bin::validate + compiler_gym::compiler_env_state + compiler_gym::envs::envs + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/requirements.txt b/compiler_gym/requirements.txt index 721c9590d1..04dfeec4f0 100644 --- a/compiler_gym/requirements.txt +++ b/compiler_gym/requirements.txt @@ -3,6 +3,7 @@ deprecated>=1.2.12 docker>=4.0.0 fasteners>=0.15 grpcio>=1.32.0 +grpcio_tools>=1.32.0 gym>=0.18.0,<0.21 humanize>=2.6.0 loop_tool_py==0.0.7 diff --git a/compiler_gym/service/CMakeLists.txt b/compiler_gym/service/CMakeLists.txt new file mode 100644 index 0000000000..9ad17ae9c8 --- /dev/null +++ b/compiler_gym/service/CMakeLists.txt @@ -0,0 +1,54 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + service + SRCS + "__init__.py" + DEPS + ::compilation_session + ::connection + compiler_gym::service::proto::proto + PUBLIC +) + +cg_py_library( + NAME + compilation_session + SRCS + "compilation_session.py" + DEPS + compiler_gym::service::proto::proto + PUBLIC +) + +cg_cc_library( + NAME + CompilationSession + HDRS + "CompilationSession.h" + SRCS + "CompilationSession.cc" + DEPS + compiler_gym::service::proto::compiler_gym_service_cc + ABS_DEPS + Boost::filesystem + grpc++ + PUBLIC +) + +cg_py_library( + NAME + connection + SRCS + "connection.py" + DEPS + compiler_gym::service::proto::proto + compiler_gym::util::util + PUBLIC +) diff --git a/compiler_gym/service/proto/CMakeLists.txt b/compiler_gym/service/proto/CMakeLists.txt new file mode 100644 index 0000000000..015d87b706 --- /dev/null +++ b/compiler_gym/service/proto/CMakeLists.txt @@ -0,0 +1,60 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME "proto" + SRCS + "__init__.py" + "py_converters.py" + PUBLIC + DEPS + "::compiler_gym_service_py" + "::compiler_gym_service_py_grpc" + compiler_gym::spaces::commandline + compiler_gym::spaces::dict + compiler_gym::spaces::discrete + compiler_gym::spaces::named_discrete + compiler_gym::spaces::scalar + compiler_gym::spaces::tuple +) + +proto_library( + NAME + compiler_gym_service + SRCS + compiler_gym_service.proto + PUBLIC +) + +py_proto_library( + NAME + compiler_gym_service_py + DEPS + ::compiler_gym_service +) + +cc_proto_library( + NAME + compiler_gym_service_cc + DEPS + ::compiler_gym_service + PUBLIC +) + +cc_grpc_library( + NAME compiler_gym_service_cc_grpc + SRCS ::compiler_gym_service + GRPC_ONLY + PUBLIC + DEPS ::compiler_gym_service_cc +) + +py_grpc_library( + NAME "compiler_gym_service_py_grpc" + SRCS "::compiler_gym_service" + DEPS "::compiler_gym_service_py" +) diff --git a/compiler_gym/service/runtime/CMakeLists.txt b/compiler_gym/service/runtime/CMakeLists.txt new file mode 100644 index 0000000000..4cf6344e3a --- /dev/null +++ b/compiler_gym/service/runtime/CMakeLists.txt @@ -0,0 +1,127 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + runtime + SRCS + "__init__.py" + DEPS + ::create_and_run_compiler_gym_service + PUBLIC +) + +cg_cc_library( + NAME + cc_runtime + HDRS + "Runtime.h" + DEPS + ::CreateAndRunCompilerGymServiceImpl + PUBLIC +) + +cg_py_library( + NAME + benchmark_cache + SRCS + "benchmark_cache.py" + DEPS + compiler_gym::service::proto::proto + PUBLIC +) + +cg_cc_library( + NAME + BenchmarkCache + HDRS + "BenchmarkCache.h" + SRCS + "BenchmarkCache.cc" + DEPS + compiler_gym::service::proto::compiler_gym_service_cc + ABS_DEPS + Boost::filesystem + grpc++ + glog::glog + PUBLIC +) + +cg_py_library( + NAME + compiler_gym_service + SRCS + "compiler_gym_service.py" + DEPS + ::benchmark_cache + compiler_gym::service::compilation_session + compiler_gym::service::proto::proto + compiler_gym::util::util + PUBLIC +) + +cg_cc_library( + NAME + CompilerGymService + HDRS + "CompilerGymService.h" + "CompilerGymServiceImpl.h" + DEPS + ::BenchmarkCache + ::CompilerGymServiceImpl + compiler_gym::service::CompilationSession + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::service::proto::compiler_gym_service_cc_grpc + ABS_DEPS + Boost::filesystem + grpc++ + PUBLIC +) + +cg_cc_library( + NAME + CompilerGymServiceImpl + HDRS + "CompilerGymServiceImpl.h" + DEPS + compiler_gym::util::GrpcStatusMacros + compiler_gym::util::Version + ABS_DEPS + fmt + glog::glog + PUBLIC +) + +cg_py_library( + NAME + create_and_run_compiler_gym_service + SRCS + "create_and_run_compiler_gym_service.py" + DEPS + ::compiler_gym_service + compiler_gym::service::proto::proto + compiler_gym::util::util + PUBLIC +) + +cg_cc_library( + NAME + CreateAndRunCompilerGymServiceImpl + HDRS + "CreateAndRunCompilerGymServiceImpl.h" + SRCS + "CreateAndRunCompilerGymServiceImpl.cc" + DEPS + ::CompilerGymService + compiler_gym::util::GrpcStatusMacros + ABS_DEPS + Boost::filesystem + grpc++ + gflags + glog::glog + PUBLIC +) diff --git a/compiler_gym/spaces/CMakeLists.txt b/compiler_gym/spaces/CMakeLists.txt new file mode 100644 index 0000000000..f209fbc967 --- /dev/null +++ b/compiler_gym/spaces/CMakeLists.txt @@ -0,0 +1,94 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + spaces + SRCS + "__init__.py" + DEPS + ::box + ::commandline + ::dict + ::discrete + ::named_discrete + ::reward + ::scalar + ::sequence + ::tuple + PUBLIC +) + +cg_py_library( + NAME box + SRCS box.py +) + +cg_py_library( + NAME + commandline + SRCS + "commandline.py" + DEPS + ::named_discrete + PUBLIC +) + +cg_py_library( + NAME dict + SRCS dict.py +) + +cg_py_library( + NAME discrete + SRCS discrete.py +) + +cg_py_library( + NAME + named_discrete + SRCS + "named_discrete.py" + DEPS + ::discrete + PUBLIC +) + +cg_py_library( + NAME + reward + SRCS + "reward.py" + DEPS + ::scalar + compiler_gym::service::service + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + scalar + SRCS + "scalar.py" + PUBLIC +) + +cg_py_library( + NAME + sequence + SRCS + "sequence.py" + DEPS + ::scalar + PUBLIC +) + +cg_py_library( + NAME tuple + SRCS "tuple.py" +) diff --git a/compiler_gym/third_party/CMakeLists.txt b/compiler_gym/third_party/CMakeLists.txt new file mode 100644 index 0000000000..8c0906a570 --- /dev/null +++ b/compiler_gym/third_party/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() diff --git a/compiler_gym/third_party/autophase/CMakeLists.txt b/compiler_gym/third_party/autophase/CMakeLists.txt new file mode 100644 index 0000000000..61ac60c25f --- /dev/null +++ b/compiler_gym/third_party/autophase/CMakeLists.txt @@ -0,0 +1,63 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + autophase + SRCS + "__init__.py" + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS analysis core support) +cg_cc_library( + NAME + InstCount + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + HDRS + "InstCount.h" + SRCS + "InstCount.cc" + ABS_DEPS + ${_LLVM_LIBS} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +llvm_map_components_to_libnames(_LLVM_LIBS analysis core irreader support) +cg_cc_binary( + NAME + compute_autophase + SRCS + "compute_autophase.cc" + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + DEPS + ::InstCount + ABS_DEPS + glog::glog + ${_LLVM_LIBS} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} +) + +cg_genrule( + NAME libLLVMPolly + OUTS "libLLVMPolly.so" + COMMAND + "cp $ $@" + ABS_DEPENDS + LLVMPolly +) diff --git a/compiler_gym/third_party/cbench/CMakeLists.txt b/compiler_gym/third_party/cbench/CMakeLists.txt new file mode 100644 index 0000000000..f87a874124 --- /dev/null +++ b/compiler_gym/third_party/cbench/CMakeLists.txt @@ -0,0 +1,237 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_genrule( + NAME cbench_tar + DEPENDS ::cbench + OUTS "llvm_bitcodes-10.0.0-cbench-v1.tar.bz2" + CMD "tar cjfh \"$@\" -C \"$(@D)\" cbench-v1" + PUBLIC +) + +cg_filegroup( + NAME cbench + DEPENDS + ::adpcm + ::bitcount + ::blowfish + ::bzip2 + ::crc32 + ::dijkstra + ::ghostscript + ::gsm + ::ispell + ::jpeg-c + ::jpeg-d + ::lame + ::patricia + ::qsort + ::rijndael + ::sha + ::stringsearch + ::stringsearch2 + ::susan + ::tiff2bw + ::tiff2rgba + ::tiffdither + ::tiffmedian + PUBLIC +) + +set(cBench_RUNTIME_DATA_SRC_FILES + ${cBench_consumer_tiff_data_FILE} + ${cBench_office_data_FILE} + ${cBench_telecom_data_FILE} + ${cBench_consumer_jpeg_data_FILE} + ${cBench_telecom_gsm_data_FILE} + ${cBench_consumer_data_FILE} + ${cBench_bzip2_data_FILE} + ${cBench_network_patricia_data_FILE} + ${cBench_network_dijkstra_data_FILE} + ${cBench_automotive_susan_data_FILE} + ${cBench_automotive_qsort_data_FILE}) +set(_CMD "cp -R \"${CMAKE_CURRENT_SOURCE_DIR}/runtime_data\" \"$@\"") +foreach(FILE_ IN LISTS cBench_RUNTIME_DATA_SRC_FILES) + string(CONCAT _CMD "${_CMD}" " && tar xzf \"${FILE_}\" -C \"$(@D)/runtime_data\"") +endforeach() +file(GLOB_RECURSE LOCAL_RUNTIME_DATA_FILES + LIST_DIRECTORIES true "${CMAKE_CURRENT_SOURCE_DIR}/runtime_data") +cg_genrule( + NAME make_runtime_data + SRCS + ${cBench_RUNTIME_DATA_SRC_FILES} + ${LOCAL_RUNTIME_DATA_FILES} + OUTS "runtime_data" + COMMAND "${_CMD}" +) + +cg_filegroup( + NAME benchmarks_list + FILES "${CMAKE_CURRENT_SOURCE_DIR}/benchmarks.txt" + PUBLIC +) + +cg_py_binary( + NAME make_llvm_module + SRCS "make_llvm_module.py" + DEPS compiler_gym::envs::llvm::llvm_benchmark +) + +set(CBENCH_MODULES + crc32 + jpeg-c + jpeg-d + stringsearch2 + adpcm + bitcount + blowfish + bzip2 + dijkstra + patricia + qsort + rijndael + sha + stringsearch + susan + tiff2bw + tiff2rgba + tiffdither + tiffmedian) +set(CBENCH_MODULE_DIRS + "${cBench_SRC_DIR}/telecom_CRC32" + "${ctuning_ai_SRC_DIR}/program/cbench-consumer-jpeg-c" + "${ctuning_ai_SRC_DIR}/program/cbench-consumer-jpeg-d" + "${ctuning_ai_SRC_DIR}/program/cbench-office-stringsearch2" + "${cBench_SRC_DIR}/telecom_adpcm_c" + "${cBench_SRC_DIR}/automotive_bitcount" + "${cBench_SRC_DIR}/security_blowfish_d" + "${cBench_SRC_DIR}/bzip2d" + "${cBench_SRC_DIR}/network_dijkstra" + "${cBench_SRC_DIR}/network_patricia" + "${cBench_SRC_DIR}/automotive_qsort1" + "${cBench_SRC_DIR}/security_rijndael_d" + "${cBench_SRC_DIR}/security_sha" + "${cBench_SRC_DIR}/office_stringsearch1" + "${cBench_SRC_DIR}/automotive_susan_c" + "${cBench_SRC_DIR}/consumer_tiff2bw" + "${cBench_SRC_DIR}/consumer_tiff2rgba" + "${cBench_SRC_DIR}/consumer_tiffdither" + "${cBench_SRC_DIR}/consumer_tiffmedian") +foreach(MODULE_ DIR_ IN ZIP_LISTS CBENCH_MODULES CBENCH_MODULE_DIRS) + file(GLOB_RECURSE MODULE_FILES LIST_DIRECTORIES true "${DIR_}/*") + string(CONCAT _CMD_PY + "\"${Python3_EXECUTABLE}\" " + "\"${CMAKE_CURRENT_BINARY_DIR}/make_llvm_module.py\" \"${DIR_}\" \"$@\"") + set_command_pythonpath(COMMAND "${_CMD_PY}" RESULT _CMD_PY) + string(CONCAT _CMD + "mkdir -p \"$(@D)\" && " + "${_CMD_PY}") + cg_genrule( + NAME ${MODULE_} + SRCS + ${MODULE_FILES} + OUTS "cbench-v1/${MODULE_}.bc" + COMMAND "${_CMD}" + DEPENDS + ::make_llvm_module + PUBLIC + ) +endforeach() + +file(GLOB_RECURSE office_ghostscript_FILES LIST_DIRECTORIES true + "${cBench_SRC_DIR}/office_ghostscript/*") +string(CONCAT _CMD + "mkdir -p \"$(@D)\" && " + "rsync -rL \"${cBench_SRC_DIR}/office_ghostscript/\" \"$(@D)/office_ghostscript_src/\" && " + "patch --quiet --forward \"$(@D)/office_ghostscript_src/src/idebug.c\" < \"${CMAKE_CURRENT_SOURCE_DIR}/cBench-ghostscript-idebug.c.patch\" && " + "patch --quiet --forward \"$(@D)/office_ghostscript_src/src/std.h\" < \"${CMAKE_CURRENT_SOURCE_DIR}/cBench-ghostscript-std.h.patch\" && ") +string(CONCAT _CMD_PY + "\"${Python3_EXECUTABLE}\" " + "\"${CMAKE_CURRENT_BINARY_DIR}/make_llvm_module.py\" \"$(@D)/office_ghostscript_src\" \"$@\"") +set_command_pythonpath(COMMAND "${_CMD_PY}" RESULT _CMD_PY) +string(CONCAT _CMD "${_CMD}" "${_CMD_PY}") +cg_genrule( + NAME ghostscript + SRCS + ${office_ghostscript_FILES} + "cBench-ghostscript-std.h.patch" + "cBench-ghostscript-idebug.c.patch" + OUTS "cbench-v1/ghostscript.bc" + COMMAND "${_CMD}" + DEPENDS + ::make_llvm_module + PUBLIC +) + +file(GLOB_RECURSE telecom_gsm_FILES LIST_DIRECTORIES true + "${cBench_SRC_DIR}/telecom_gsm/*") +string(CONCAT _CMD + "mkdir -p \"$(@D)\" && " + "rsync -rL \"${cBench_SRC_DIR}/telecom_gsm/\" \"$(@D)/telecom_gsm_src/\" && " + "patch --quiet --forward \"$(@D)/telecom_gsm_src/src/add.c\"" + " < \"${CMAKE_CURRENT_SOURCE_DIR}/cBench-gsm-add.c.patch\" && ") +string(CONCAT _CMD_PY + "\"${Python3_EXECUTABLE}\" " + "\"${CMAKE_CURRENT_BINARY_DIR}/make_llvm_module.py\" \"$(@D)/telecom_gsm_src\" \"$@\" " + "-DSASR -DSTUPID_COMPILER -DNeedFunctionPrototypes=1") +set_command_pythonpath(COMMAND "${_CMD_PY}" RESULT _CMD_PY) +string(CONCAT _CMD "${_CMD}" "${_CMD_PY}") +cg_genrule( + NAME gsm + SRCS + ${telecom_gsm_FILES} + "cBench-gsm-add.c.patch" + OUTS "cbench-v1/gsm.bc" + COMMAND "${_CMD}" + DEPENDS + ::make_llvm_module + PUBLIC +) + +file(GLOB_RECURSE office_ispell_FILES LIST_DIRECTORIES true + "${cBench_SRC_DIR}/office_ispell/*") +string(CONCAT _CMD + "mkdir -p \"$(@D)\" && " + "rsync -rL \"${cBench_SRC_DIR}/office_ispell/\" \"$(@D)/office_ispell_src/\" && " + "patch --quiet --forward \"$(@D)/office_ispell_src/src/correct.c\" < \"${CMAKE_CURRENT_SOURCE_DIR}/cBench-ispell-correct.c.patch\" && ") +string(CONCAT _CMD_PY + "\"${Python3_EXECUTABLE}\" " + "\"${CMAKE_CURRENT_BINARY_DIR}/make_llvm_module.py\" \"$(@D)/office_ispell_src\" \"$@\"") +set_command_pythonpath(COMMAND "${_CMD_PY}" RESULT _CMD_PY) +string(CONCAT _CMD "${_CMD}" "${_CMD_PY}") +cg_genrule( + NAME ispell + SRCS + ${office_ispell_FILES} + "cBench-ispell-correct.c.patch" + OUTS "cbench-v1/ispell.bc" + COMMAND "${_CMD}" + DEPENDS + ::make_llvm_module + PUBLIC +) + +file(GLOB_RECURSE consumer_lame_FILES LIST_DIRECTORIES true + "${cBench_SRC_DIR}/consumer_lame/*") +string(CONCAT _CMD + "mkdir -p \"$(@D)\" && ") +string(CONCAT _CMD_PY + "\"${Python3_EXECUTABLE}\" " + "\"${CMAKE_CURRENT_BINARY_DIR}/make_llvm_module.py\" " + "\"${cBench_SRC_DIR}/consumer_lame\" \"$@\" -DLAMESNDFILE -DHAVEMPGLIB -DLAMEPARSE") +set_command_pythonpath(COMMAND "${_CMD_PY}" RESULT _CMD_PY) +string(CONCAT _CMD "${_CMD}" "${_CMD_PY}") +cg_genrule( + NAME lame + SRCS + ${consumer_lame_FILES} + OUTS "cbench-v1/lame.bc" + COMMAND "${_CMD}" + DEPENDS + ::make_llvm_module + PUBLIC +) diff --git a/compiler_gym/third_party/csmith/CMakeLists.txt b/compiler_gym/third_party/csmith/CMakeLists.txt new file mode 100644 index 0000000000..ec52730010 --- /dev/null +++ b/compiler_gym/third_party/csmith/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_filegroup(NAME all FILES "${Csmith_ROOT_DIR}") diff --git a/compiler_gym/third_party/inst2vec/CMakeLists.txt b/compiler_gym/third_party/inst2vec/CMakeLists.txt new file mode 100644 index 0000000000..b1f1659d3c --- /dev/null +++ b/compiler_gym/third_party/inst2vec/CMakeLists.txt @@ -0,0 +1,52 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_genrule( + NAME dictionary + OUTS "dictionary.pickle" + SRCS "dictionary.tar.bz2" + COMMAND "tar xjf ${CMAKE_CURRENT_SOURCE_DIR}/dictionary.tar.bz2 -C $(@D)" +) + +cg_genrule( + NAME embeddings + OUTS "embeddings.pickle" + SRCS "embeddings.tar.bz2" + COMMAND "tar xjf ${CMAKE_CURRENT_SOURCE_DIR}/embeddings.tar.bz2 -C $(@D)" +) + +cg_py_library( + NAME + inst2vec + SRCS + "__init__.py" + DATA + ::dictionary + ::embeddings + DEPS + ::inst2vec_preprocess + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + inst2vec_preprocess + SRCS + "inst2vec_preprocess.py" + DEPS + ::rgx_utils + PUBLIC +) + +cg_py_library( + NAME + rgx_utils + SRCS + "rgx_utils.py" + PUBLIC +) diff --git a/compiler_gym/third_party/llvm/CMakeLists.txt b/compiler_gym/third_party/llvm/CMakeLists.txt new file mode 100644 index 0000000000..700e2dede1 --- /dev/null +++ b/compiler_gym/third_party/llvm/CMakeLists.txt @@ -0,0 +1,81 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + llvm + SRCS + "__init__.py" + DEPS + compiler_gym::util::util + PUBLIC +) + +llvm_map_components_to_libnames(llvm_libs support irreader core) +cg_cc_binary( + NAME + compute_ir_instruction_count + SRCS + "compute_ir_instruction_count.cc" + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + ABS_DEPS + glog::glog + ${llvm_libs} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} +) + +cg_genrule( + NAME libLLVMPolly + OUTS "libLLVMPolly.so" + COMMAND + "cp $ $@" + ABS_DEPENDS + LLVMPolly +) + +cg_py_library( + NAME + instcount + SRCS + "instcount.py" + PUBLIC +) + +llvm_map_components_to_libnames(llvm_libs support core analysis) +cg_cc_library( + NAME + InstCount + COPTS + "-DGOOGLE_PROTOBUF_NO_RTTI" + "-fno-rtti" + HDRS + "InstCount.h" + SRCS + "InstCount.cc" + ABS_DEPS + glog::glog + ${llvm_libs} + INCLUDES + ${LLVM_INCLUDE_DIRS} + DEFINES + ${LLVM_DEFINITIONS} + PUBLIC +) + +cg_cc_binary( + NAME + PrintInstCountFeatureNames + SRCS + "PrintInstCountFeatureNames.cc" + DEPS + ::InstCount +) diff --git a/compiler_gym/third_party/neuro-vectorizer/CMakeLists.txt b/compiler_gym/third_party/neuro-vectorizer/CMakeLists.txt new file mode 100644 index 0000000000..60db208cc1 --- /dev/null +++ b/compiler_gym/third_party/neuro-vectorizer/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_filegroup( + NAME header + FILES "${CMAKE_CURRENT_LIST_DIR}/header.h" +) diff --git a/compiler_gym/util/CMakeLists.txt b/compiler_gym/util/CMakeLists.txt new file mode 100644 index 0000000000..5bffc6addd --- /dev/null +++ b/compiler_gym/util/CMakeLists.txt @@ -0,0 +1,136 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + util + SRCS + "__init__.py" + "capture_output.py" + "commands.py" + "debug_util.py" + "decorators.py" + "download.py" + "executor.py" + "filesystem.py" + "gym_type_hints.py" + "logging.py" + "logs.py" + "minimize_trajectory.py" + "parallelization.py" + "registration.py" + "runfiles_path.py" + "shell_format.py" + "statistics.py" + "tabulate.py" + "temporary_working_directory.py" + "thread_pool.py" + "timer.py" + "truncate.py" + GENERATED_SRCS + "$" + DEPS + make_version + # TODO(boian): verify if needed + #unconverted_name:@rules_python//python/runfiles + PUBLIC +) + +cg_cc_library( + NAME + EnumUtil + SRCS + "EnumUtil.h" + ABS_DEPS + grpc++ + fmt + magic_enum + PUBLIC +) + +cg_cc_library( + NAME + GrpcStatusMacros + HDRS + "GrpcStatusMacros.h" + ABS_DEPS + Boost::headers + grpc++ + fmt + glog::glog + PUBLIC +) + +cg_cc_library( + NAME + RunfilesPath + HDRS + "RunfilesPath.h" + SRCS + "RunfilesPath.cc" + ABS_DEPS + Boost::filesystem + fmt + PUBLIC +) + +cg_cc_library( + NAME + StrLenConstexpr + HDRS + "StrLenConstexpr.h" + PUBLIC +) + +cg_cc_library( + NAME + Subprocess + HDRS + "Subprocess.h" + SRCS + "Subprocess.cc" + DEPS + compiler_gym::service::proto::compiler_gym_service_cc + ABS_DEPS + Boost::filesystem + Boost::headers + grpc++ + fmt + PUBLIC +) + +cg_cc_library( + NAME + Unreachable + HDRS + "Unreachable.h" + ABS_DEPS + glog::glog + PUBLIC +) + +cg_genrule( + NAME make_version + OUTS "version.py" + SRCS "${CMAKE_CURRENT_SOURCE_DIR}/../../VERSION" + COMMAND "echo \"__version__ = \\\"$(cat \"${CMAKE_CURRENT_SOURCE_DIR}/../../VERSION\")\\\"\" > $@" +) + +cg_cc_library( + NAME + Version + HDRS + "$/Version.h" + PUBLIC +) + +cg_genrule( + NAME make_version_header + OUTS "Version.h" + SRCS "${CMAKE_CURRENT_SOURCE_DIR}/../../VERSION" + COMMAND "echo \"#define COMPILER_GYM_VERSION \\\"$(cat \"${CMAKE_CURRENT_SOURCE_DIR}/../../VERSION\")\\\"\" > $@" +) diff --git a/compiler_gym/util/flags/CMakeLists.txt b/compiler_gym/util/flags/CMakeLists.txt new file mode 100644 index 0000000000..3b748ce48d --- /dev/null +++ b/compiler_gym/util/flags/CMakeLists.txt @@ -0,0 +1,26 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + flags + SRCS + "benchmark_from_flags.py" + "env_from_flags.py" + "episode_length.py" + "episodes.py" + "learning_rate.py" + "nproc.py" + "output_dir.py" + "seed.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::envs + compiler_gym::service::service + compiler_gym::service::proto::proto + PUBLIC +) diff --git a/compiler_gym/views/CMakeLists.txt b/compiler_gym/views/CMakeLists.txt new file mode 100644 index 0000000000..f41e879dda --- /dev/null +++ b/compiler_gym/views/CMakeLists.txt @@ -0,0 +1,56 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + views + SRCS + "__init__.py" + DEPS + ::observation + ::reward + PUBLIC +) + +cg_py_library( + NAME + observation + SRCS + "observation.py" + DEPS + ::observation_space_spec + compiler_gym::service::service + compiler_gym::service::proto::proto + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + observation_space_spec + SRCS + "observation_space_spec.py" + DEPS + compiler_gym::service::service + compiler_gym::service::proto::proto + compiler_gym::spaces::spaces + compiler_gym::util::util + PUBLIC +) + +cg_py_library( + NAME + reward + SRCS + "reward.py" + DEPS + ::observation + compiler_gym::datasets::datasets + compiler_gym::service::proto::proto + compiler_gym::spaces::spaces + PUBLIC +) diff --git a/compiler_gym/wrappers/CMakeLists.txt b/compiler_gym/wrappers/CMakeLists.txt new file mode 100644 index 0000000000..326ba828e0 --- /dev/null +++ b/compiler_gym/wrappers/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + wrappers + SRCS + "__init__.py" + "commandline.py" + "core.py" + "datasets.py" + "llvm.py" + "time_limit.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::envs + compiler_gym::util::util + compiler_gym::views::views + PUBLIC +) diff --git a/external/absl/CMakeLists.txt b/external/absl/CMakeLists.txt new file mode 100644 index 0000000000..0aa5c47c78 --- /dev/null +++ b/external/absl/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(abls) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +ExternalProject_Add( + absl + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/absl" + URL "https://github.com/abseil/abseil-cpp/archive/997aaf3a28308eba1b9156aa35ab7bca9688e9f6.tar.gz" + URL_HASH "SHA256=35f22ef5cb286f09954b7cc4c85b5a3f6221c9d4df6b8c4a1e9d399555b366ee" + CMAKE_ARGS + -C "${CMAKE_CURRENT_BINARY_DIR}/absl_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" + -DCMAKE_POSITION_INDEPENDENT_CODE=ON +) diff --git a/external/boost/CMakeLists.txt b/external/boost/CMakeLists.txt new file mode 100644 index 0000000000..d889dc6619 --- /dev/null +++ b/external/boost/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(protobuf) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) +include(ProcessorCount) + +find_package(Git REQUIRED) + +if(DEFINED CMAKE_C_FLAGS) + list(APPEND _ADDITIONAL_ARGS "cflags=\"${CMAKE_C_FLAGS}\"") +endif() +if(DEFINED CMAKE_CXX_FLAGS) + list(APPEND _ADDITIONAL_ARGS "cxxflags=\"${CMAKE_CXX_FLAGS}\"") +endif() +if (DEFINED CMAKE_STATIC_LINKER_FLAGS_INIT OR + DEFINED CMAKE_SHARED_LINKER_FLAGS_INIT OR + DEFINED CMAKE_STATIC_LINKER_FLAGS OR + DEFINED CMAKE_SHARED_LINKER_FLAGS) + list(APPEND _ADDITIONAL_ARGS "linkflags=\"${CMAKE_STATIC_LINKER_FLAGS_INIT} ${CMAKE_SHARED_LINKER_FLAGS_INIT} ${CMAKE_STATIC_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}\"") +endif() + +ProcessorCount(_JOBS) +ExternalProject_Add( + boost + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/boost" + GIT_REPOSITORY "https://github.com/boostorg/boost.git" + GIT_TAG afb333b7c5101041f0280b2edf155c55114c9c95 #tag: boost-1.71.0 + BUILD_IN_SOURCE TRUE + CONFIGURE_COMMAND + "${CMAKE_COMMAND}" -E env "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" + ./bootstrap.sh + BUILD_COMMAND + "${CMAKE_COMMAND}" -E env "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" + ./b2 + -j${_JOBS} + "--prefix=${CMAKE_INSTALL_PREFIX}" + "--build-dir=${CMAKE_CURRENT_BINARY_DIR}/build" + --with-filesystem + --with-headers + cxxstd=${CMAKE_CXX_STANDARD} + install + INSTALL_COMMAND "" +) diff --git a/external/cpuinfo/CMakeLists.txt b/external/cpuinfo/CMakeLists.txt new file mode 100644 index 0000000000..090095318d --- /dev/null +++ b/external/cpuinfo/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +ExternalProject_Add( + cpuinfo + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/cpuinfo" + GIT_REPOSITORY "https://github.com/pytorch/cpuinfo.git" + GIT_TAG 2e79955ecaec85da13ac8f1245a8b2afa10d31c2 + CMAKE_ARGS + -C "${CMAKE_CURRENT_BINARY_DIR}/cpuinfo_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" +) diff --git a/external/csmith/CMakeLists.txt b/external/csmith/CMakeLists.txt new file mode 100644 index 0000000000..bd58189fc5 --- /dev/null +++ b/external/csmith/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +ExternalProject_Add( + csmith + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/csmith" + GIT_REPOSITORY "https://github.com/csmith-project/csmith.git" + GIT_TAG 30dccd73b78652c4719f36572994778a5b233a4e #tag csmith-2.3.0 + CMAKE_ARGS + -C "${CMAKE_CURRENT_BINARY_DIR}/csmith_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" + "-DCOMPILE_DEFINITIONS=_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES" +) diff --git a/external/external.cmake b/external/external.cmake new file mode 100644 index 0000000000..6185c3dd12 --- /dev/null +++ b/external/external.cmake @@ -0,0 +1,481 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +include(ExternalProject) +include(FetchContent) +include(write_cache_script) +include(build_external_cmake_project) + +unset(FETCH_CONTENT_LIST) + +# # === Google test === + +set(COMPILER_GYM_GTEST_PROVIDER "internal" CACHE STRING "Find or build gtest together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_GTEST_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_GTEST_PROVIDER STREQUAL "internal") + FetchContent_Declare( + gtest + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/gtest" + GIT_REPOSITORY "https://github.com/google/googletest.git" + GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e #tag release-1.10.0 + ) + FetchContent_MakeAvailable(gtest) + add_library(GTest::GTest ALIAS gtest) + add_library(GTest::Main ALIAS gtest_main) +else() + find_package(GTest REQUIRED) +endif() + +# # === Google benchmark === + +set(COMPILER_GYM_BENCHMARK_PROVIDER "internal" CACHE STRING "Find or build benchmark together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_BENCHMARK_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_BENCHMARK_PROVIDER STREQUAL "internal") + FetchContent_Declare( + benchmark + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/benchmark" + GIT_REPOSITORY "https://github.com/google/benchmark.git" + GIT_TAG 9913418d323e64a0111ca0da81388260c2bbe1e9 #tag v1.4.0 + ) + + if(NOT benchmark_POPULATED) + FetchContent_Populate(benchmark) + + # Benchmark v1.4.0 requires C++03. + set(_CMAKE_CXX_STANDARD_OLD ${CMAKE_CXX_STANDARD}) + unset(CMAKE_CXX_STANDARD CACHE) + + option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." OFF) + + add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR}) + + set(CMAKE_CXX_STANDARD ${_CMAKE_CXX_STANDARD_OLD} CACHE STRING "C++ standard to be used." FORCE) + endif() +else() + find_package(benchmark REQUIRED) +endif() + +# # === Abseil === + +set(COMPILER_GYM_ABSEIL_PROVIDER "internal" CACHE STRING "Find or build abseil together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_ABSEIL_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_ABSEIL_PROVIDER STREQUAL "internal") + build_external_cmake_project( + NAME absl + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/absl") +endif() +find_package(absl REQUIRED) + +# # === Google flags === + +set(COMPILER_GYM_GFLAGS_PROVIDER "internal" CACHE STRING "Find or build gflags together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_GFLAGS_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_GFLAGS_PROVIDER STREQUAL "internal") + build_external_cmake_project( + NAME gflags + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/gflags") +endif() +find_package(gflags REQUIRED) + + +# # === Google logging === + +set(COMPILER_GYM_GLOG_PROVIDER "internal" CACHE STRING "Find or build glog together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_GLOG_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_GLOG_PROVIDER STREQUAL "internal") + FetchContent_Declare( + glog + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/glog" + GIT_REPOSITORY "https://github.com/google/glog.git" + GIT_TAG 96a2f23dca4cc7180821ca5f32e526314395d26a #tag v0.4.0 + ) + list(APPEND FETCH_CONTENT_LIST glog) +else() + find_package(glog REQUIRED) +endif() + +# # C++ subprocess management. https://github.com/arun11299/cpp-subprocess + +set(COMPILER_GYM_SUBPROCESS_PROVIDER "internal" CACHE STRING "Find or build subprocess together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_SUBPROCESS_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_SUBPROCESS_PROVIDER STREQUAL "internal") + build_external_cmake_project( + NAME subprocess + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/subprocess" + ) +endif() +find_package(Subprocess REQUIRED) + +# # === LLVM === + +set(COMPILER_GYM_LLVM_PROVIDER "internal" CACHE STRING "Find or build llvm together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_LLVM_PROVIDER PROPERTY STRINGS "internal" "external") +build_external_cmake_project( + NAME llvm + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/llvm" + CONFIG_ARGS "-DCOMPILER_GYM_LLVM_PROVIDER=${COMPILER_GYM_LLVM_PROVIDER}") +set(LLVM_SRC_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/llvm/llvm/src/llvm") +find_package(LLVM 10.0.0 EXACT REQUIRED) +# In a bunch of places in the code it is used "#include " +list(APPEND LLVM_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/external/llvm/install") + + +# # === Protocol buffers === + +set(COMPILER_GYM_PROTOBUF_PROVIDER "internal" CACHE STRING "Find or build protobuf together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_PROTOBUF_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_PROTOBUF_PROVIDER STREQUAL "internal") + write_cache_script("${CMAKE_CURRENT_BINARY_DIR}/external/protobuf/protobuf_initial_cache.cmake") + execute_process( + COMMAND "${CMAKE_COMMAND}" + -C "${CMAKE_CURRENT_BINARY_DIR}/external/protobuf/protobuf_initial_cache.cmake" + -S "${CMAKE_CURRENT_LIST_DIR}/protobuf" + -B "${CMAKE_CURRENT_BINARY_DIR}/external/protobuf" + -D "CMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/external/protobuf/install" + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" + --build "${CMAKE_CURRENT_BINARY_DIR}/external/protobuf" + COMMAND_ERROR_IS_FATAL ANY + ) + list(PREPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/external/protobuf/install") +endif() +find_package(Protobuf REQUIRED) + +# # === GRPC === + +set(COMPILER_GYM_GRPC_PROVIDER "internal" CACHE STRING "Find or build gRPC together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_GRPC_PROVIDER PROPERTY STRINGS "internal" "external") + +set(gRPC_ABSL_PROVIDER package) +if(COMPILER_GYM_GRPC_PROVIDER STREQUAL "internal") + if (NOT DEFINED gRPC_ABSL_PROVIDER OR gRPC_ABSL_PROVIDER STREQUAL "module") + list(APPEND _gRPC_GIT_SUBMODULES "third_party/abseil-cpp") + endif() + + if (NOT DEFINED gRPC_ZLIB_PROVIDER OR gRPC_ZLIB_PROVIDER STREQUAL "module") + list(APPEND _gRPC_GIT_SUBMODULES "third_party/zlib") + endif() + + if (NOT DEFINED gRPC_CARES_PROVIDER OR gRPC_CARES_PROVIDER STREQUAL "module") + list(APPEND _gRPC_GIT_SUBMODULES "third_party/cares/cares") + endif() + + if (NOT DEFINED gRPC_RE2_PROVIDER OR gRPC_RE2_PROVIDER STREQUAL "module") + list(APPEND _gRPC_GIT_SUBMODULES "third_party/re2") + endif() + + if (NOT DEFINED gRPC_SSL_PROVIDER OR gRPC_SSL_PROVIDER STREQUAL "module") + list(APPEND _gRPC_GIT_SUBMODULES "third_party/boringssl-with-bazel") + endif() + + set(gRPC_PROTOBUF_PROVIDER "package" CACHE STRING "") + + # In CMake v3.19.6 if GIT_SUBMODULES changes during reconfiguration + # the FetchContent will not populate new submodules. + # The PREFIX directory will have to be deleted manually. + FetchContent_Declare( + grpc + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/grpc" + GIT_REPOSITORY "https://github.com/grpc/grpc.git" + GIT_TAG 736e3758351ced3cd842bad3ba4e2540f01bbc48 # v1.36.0 + GIT_SUBMODULES ${_gRPC_GIT_SUBMODULES} + ) + FetchContent_MakeAvailable(grpc) + set(_GRPC_CPP_PLUGIN_EXECUTABLE $) + #TODO(boian): remove this when GrpcStatusMacros.h uses the correct include path. + target_include_directories(grpc++ INTERFACE "${grpc_SOURCE_DIR}") +else() + find_package(gRPC REQUIRED) + set(_GRPC_CPP_PLUGIN_EXECUTABLE $) +endif() + +# # === C++ enum trickery === +# # https://github.com/Neargye/magic_enum + +set(COMPILER_GYM_MAGIC_ENUM_PROVIDER "internal" CACHE STRING "Find or build magic_enum together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_MAGIC_ENUM_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_MAGIC_ENUM_PROVIDER STREQUAL "internal") + FetchContent_Declare( + magic_enum + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/magic_enum" + GIT_REPOSITORY "https://github.com/Neargye/magic_enum.git" + GIT_TAG 6e932ef66dbe054e039d4dba77a41a12f9f52e0c #tag 0.7.3 + ) + list(APPEND FETCH_CONTENT_LIST magic_enum) +else() + find_package(magic_enum REQUIRED) +endif() + +# # === ctuning-programs === +# # https://github.com/ChrisCummins/ctuning-programs + +# This seems to be unused. +#ExternalProject_Add( +# ctuning-programs +# PREFIX "${CMAKE_BINARY_DIR}/ctuning-programs" +# URL "https://github.com/ChrisCummins/ctuning-programs/archive/c3c126fcb400f3a14b69b152f15d15eae78ef908.tar.gz" +# URL_HASH "SHA256=5e14a49f87c70999a082cb5cf19b780d0b56186f63356f8f994dd9ffc79ec6f3" +# CONFIGURE_COMMAND "" +# BUILD_COMMAND "" +# INSTALL_COMMAND "" +#) + +file(GLOB CTUNING-PROGRAMS-SRCS "ctuning-programs/**") + +source_group( + ctuning-programs-all + FILES CTUNING-PROGRAMS-SRCS +) + +source_group( + ctuning-programs-readme + FILES "ctuning-programs/README.md" +) + +# # === cBench === +# # https://ctuning.org/wiki/index.php/CTools:CBench + +FetchContent_Declare( + cBench + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cbench" + URL "https://dl.fbaipublicfiles.com/compiler_gym/cBench_V1.1.tar.gz" + URL_HASH "SHA256=8908d742f5223f09f9a4d10f7e06bc805a0c1694aa70974d2aae91ab627b51e6" + DOWNLOAD_NO_EXTRACT FALSE +) +FetchContent_MakeAvailable(cBench) +FetchContent_GetProperties(cBench SOURCE_DIR cBench_SRC_DIR) + +FetchContent_Declare( + ctuning-ai + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/ctuning-ai" + URL "https://github.com/ChrisCummins/ck-mlops/archive/406738ad6d1fb2c1da9daa2c09d26fccab4e0938.tar.gz" + URL_HASH "SHA256=a82c13733696c46b5201c614fcf7229c3a74a83ce485cab2fbf17309b7564f9c" +) +FetchContent_MakeAvailable(ctuning-ai) +FetchContent_GetProperties(ctuning-ai SOURCE_DIR ctuning_ai_SRC_DIR) + +# # Datasets. + +FetchContent_Declare( + cBench_consumer_tiff_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_tiff_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_consumer_tiff_data.tar.gz" + URL_HASH "SHA256=779abb7b7fee8733313e462e6066c16375e9209a9f7ff692fd06c7598946939a" +) +FetchContent_MakeAvailable(cBench_consumer_tiff_data) +set(cBench_consumer_tiff_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_tiff_data/src/cDatasets_V1.1_consumer_tiff_data.tar.gz") + +FetchContent_Declare( + cBench_office_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_office_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_office_data.tar.gz" + URL_HASH "SHA256=cfa09cd37cb93aba57415033905dc6308653c7b833feba5a25067bfb62999f32" +) +FetchContent_MakeAvailable(cBench_office_data) +set(cBench_office_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_office_data/src/cDatasets_V1.1_office_data.tar.gz") + +FetchContent_Declare( + cBench_telecom_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_telecom_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_telecom_data.tar.gz" + URL_HASH "SHA256=e5cb6663beefe32fd12f90c8f533f8e1bce2f05ee4e3836efb5556d5e1089df0" +) +FetchContent_MakeAvailable(cBench_telecom_data) +set(cBench_telecom_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_telecom_data/src/cDatasets_V1.1_telecom_data.tar.gz") + +FetchContent_Declare( + cBench_consumer_jpeg_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_jpeg_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_consumer_jpeg_data.tar.gz" + URL_HASH "SHA256=bec5ffc15cd2f952d9a786f3cd31d90955c318a5e4f69c5ba472f79d5a3e8f0b" +) +FetchContent_MakeAvailable(cBench_consumer_jpeg_data) +set(cBench_consumer_jpeg_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_jpeg_data/src/cDatasets_V1.1_consumer_jpeg_data.tar.gz") + +FetchContent_Declare( + cBench_telecom_gsm_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_telecom_gsm_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_telecom_gsm_data.tar.gz" + URL_HASH "SHA256=52545d3a0ce15021131c62d96d3a3d7e6670e2d6c34226ac9a3d5191a1ee214a" +) +FetchContent_MakeAvailable(cBench_telecom_gsm_data) +set(cBench_telecom_gsm_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_telecom_gsm_data/src/cDatasets_V1.1_telecom_gsm_data.tar.gz") + +FetchContent_Declare( + cBench_consumer_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_consumer_data.tar.gz" + URL_HASH "SHA256=a4d40344af3022bfd7b4c6fcf6d59d598825b07d9e37769dbf1b3effa39aa445" +) +FetchContent_MakeAvailable(cBench_consumer_data) +set(cBench_consumer_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_consumer_data/src/cDatasets_V1.1_consumer_data.tar.gz") + +FetchContent_Declare( + cBench_bzip2_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_bzip2_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_bzip2_data.tar.gz" + URL_HASH "SHA256=46e5760eeef77e6b0c273af92de971bc45f33a59e0efc183073d9aa6b716c302" +) +FetchContent_MakeAvailable(cBench_bzip2_data) +set(cBench_bzip2_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_bzip2_data/src/cDatasets_V1.1_bzip2_data.tar.gz") + +FetchContent_Declare( + cBench_network_patricia_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_network_patricia_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_network_patricia_data.tar.gz" + URL_HASH "SHA256=72dae0e670d93ef929e50aca7a138463e0915502281ccafe793e378cb2a85dfb" +) +FetchContent_MakeAvailable(cBench_network_patricia_data) +set(cBench_network_patricia_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_network_patricia_data/src/cDatasets_V1.1_network_patricia_data.tar.gz") + +FetchContent_Declare( + cBench_network_dijkstra_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_network_dijkstra_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_network_dijkstra_data.tar.gz" + URL_HASH "SHA256=41c13f59cdfbc772081cd941f499b030370bc570fc2ba60a5c4b7194bc36ca5f" +) +FetchContent_MakeAvailable(cBench_network_dijkstra_data) +set(cBench_network_dijkstra_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_network_dijkstra_data/src/cDatasets_V1.1_network_dijkstra_data.tar.gz") + +FetchContent_Declare( + cBench_automotive_susan_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_automotive_susan_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_automotive_susan_data.tar.gz" + URL_HASH "SHA256=df56e1e44ccc560072381cdb001d770003ac74f92593dd5dbdfdd4ff9332a8e6" +) +FetchContent_MakeAvailable(cBench_automotive_susan_data) +set(cBench_automotive_susan_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_automotive_susan_data/src/cDatasets_V1.1_automotive_susan_data.tar.gz") + +FetchContent_Declare( + cBench_automotive_qsort_data + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_automotive_qsort_data" + URL "https://downloads.sourceforge.net/project/cbenchmark/cDatasets/V1.1/cDatasets_V1.1_automotive_qsort_data.tar.gz" + URL_HASH "SHA256=510b4225021408ac190f6f793e7d7171d3553c9916cfa8b2fb4ace005105e768" +) +FetchContent_MakeAvailable(cBench_automotive_qsort_data) +set(cBench_automotive_qsort_data_FILE + "${CMAKE_CURRENT_BINARY_DIR}/external/cBench_automotive_qsort_data/src/cDatasets_V1.1_automotive_qsort_data.tar.gz") + +# # === C++ cpuinfo === + +set(COMPILER_GYM_CPUINFO_PROVIDER "internal" CACHE STRING "Find or build cpuinfo together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_CPUINFO_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_CPUINFO_PROVIDER STREQUAL "internal") + build_external_cmake_project( + NAME cpuinfo + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/cpuinfo") +endif() +set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH TRUE) +find_package(PkgConfig REQUIRED) +pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo) +add_library(CpuInfo::cpuinfo ALIAS PkgConfig::CpuInfo) + +find_package(Clog REQUIRED) +# For some reason this does not propagate to the linker when CpuInfo::cpuinfo is included +#get_target_property(_CpuInfo_LINK_LIBS PkgConfig::CpuInfo IMPORTED_LINK_INTERFACE_LIBRARIES) +#if (NOT _CpuInfo_LINK_LIBS) +# set(_CpuInfo_LINK_LIBS Clog::libclog) +#else() +# list(APPEND _CpuInfo_LINK_LIBS Clog::libclog) +#endif() +#set_target_properties(PkgConfig::CpuInfo +# PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES +# "${_CpuInfo_LINK_LIBS}") + + +# # === Csmith === +# # https://embed.cs.utah.edu/csmith/ + +build_external_cmake_project( + NAME csmith + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/csmith" + INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/csmith/install/csmith") +find_package(Csmith REQUIRED) + +# # === DeepDataFlow === +# # https://zenodo.org/record/4122437 + +#FetchContent_Declare( +# DeepDataFlow +# PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/DeepDataFlow" +# SOURCE_DIR "${CMAKE_BINARY_DIR}/compiler_gym/third_party/DeepDataFlow" +# URL "https://zenodo.org/record/4122437/files/llvm_bc_20.06.01.tar.bz2?download=1" +# URL_HASH "SHA256=ea6accbeb005889db3ecaae99403933c1008e0f2f4adc3c4afae3d7665c54004" +#) +#list(APPEND FETCH_CONTENT_LIST DeepDataFlow) + +# === A modern C++ formatting library === +# https://fmt.dev + +set(COMPILER_GYM_FMT_PROVIDER "internal" CACHE STRING "Find or build fmt together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_FMT_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_FMT_PROVIDER STREQUAL "internal") + FetchContent_Declare( + fmt + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/fmt" + GIT_REPOSITORY "https://github.com/fmtlib/fmt.git" + GIT_TAG f94b7364b9409f05207c3af3fa4666730e11a854 #tag 6.1.2 + ) + FetchContent_MakeAvailable(fmt) +else() + find_package(fmt REQUIRED) +endif() + +# # === Boost === + +set(COMPILER_GYM_BOOST_PROVIDER "internal" CACHE STRING "Find or build boost together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_BOOST_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_BOOST_PROVIDER STREQUAL "internal") + build_external_cmake_project( + NAME boost + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/boost") +endif() +find_package(Boost REQUIRED COMPONENTS filesystem headers) + +# # === nlohmann_json === + +set(COMPILER_GYM_NLOHMANN_JSON_PROVIDER "internal" CACHE STRING "Find or build nlohmann_json together with Compiler Gym.") +set_property(CACHE COMPILER_GYM_NLOHMANN_JSON_PROVIDER PROPERTY STRINGS "internal" "external") +if(COMPILER_GYM_NLOHMANN_JSON_PROVIDER STREQUAL "internal") + FetchContent_Declare( + nlohmann_json + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/nlohmann_json" + GIT_REPOSITORY "https://github.com/nlohmann/json.git" + GIT_TAG e7b3b40b5a95bc74b9a7f662830a27c49ffc01b4 #tag: v3.7.3 + ) + list(APPEND FETCH_CONTENT_LIST nlohmann_json) +else() + find_package(nlohmann_json REQUIRED) +endif() + +# # === ProGraML === +# # https://github.com/ChrisCummins/ProGraML + +build_external_cmake_project( + NAME programl + SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/programl") +list(PREPEND CMAKE_PREFIX_PATH + "${CMAKE_CURRENT_BINARY_DIR}/external/programl/programl/src/programl/bazel-bin" + "${CMAKE_CURRENT_BINARY_DIR}/external/programl/programl/src/programl/bazel-bin/external/labm8" + "${CMAKE_CURRENT_BINARY_DIR}/external/programl/programl/src/programl/bazel-programl" + "${CMAKE_CURRENT_BINARY_DIR}/external/programl/programl/src/programl/bazel-programl/external/labm8" + ) +find_package(Labm8 REQUIRED) +find_package(ProGraML REQUIRED) + +FetchContent_MakeAvailable(${FETCH_CONTENT_LIST}) diff --git a/external/gflags/CMakeLists.txt b/external/gflags/CMakeLists.txt new file mode 100644 index 0000000000..48a1e1a200 --- /dev/null +++ b/external/gflags/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +# FetchContent is not used here because for some reason the install step +# fails with not installing all necessary files in CMake v3.19.6. +ExternalProject_Add( + gflags + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/gflags" + GIT_REPOSITORY "https://github.com/gflags/gflags.git" + GIT_TAG e171aa2d15ed9eb17054558e0b3a6a413bb01067 #tag v2.2.2 + CMAKE_ARGS + -C "${CMAKE_CURRENT_BINARY_DIR}/gflags_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" +) diff --git a/external/llvm/CMakeLists.txt b/external/llvm/CMakeLists.txt new file mode 100644 index 0000000000..ce34654631 --- /dev/null +++ b/external/llvm/CMakeLists.txt @@ -0,0 +1,53 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +include(ProcessorCount) +if (DEFINED ENV{CMAKE_BUILD_PARALLEL_LEVEL}) + set(_JOBS $ENV{CMAKE_BUILD_PARALLEL_LEVEL}) +else() + ProcessorCount(_JOBS) +endif() + +# LLVM is memory hungry during linking +cmake_host_system_information(RESULT _RAM QUERY AVAILABLE_PHYSICAL_MEMORY) +set(_RAM_PER_LINK_JOB 10240) # 10 GiB +math(EXPR _LINK_JOBS "${_RAM} / ${_RAM_PER_LINK_JOB}" OUTPUT_FORMAT DECIMAL) +if (NOT _LINK_JOBS) + set(_LINK_JOBS 1) +endif() + +# The source is still required even if version is external. +if(NOT COMPILER_GYM_LLVM_PROVIDER STREQUAL "internal") + set(_BUILD_COMMAND "") + set(_INSTALL_COMMAND "") +else() + set(_BUILD_COMMAND "${CMAKE_COMMAND}" --build "") + set(_INSTALL_COMMAND "${CMAKE_COMMAND}" --install "") +endif() + +file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/llvm_initial_cache.cmake" "set(LLVM_ENABLE_PROJECTS \"clang;polly\" CACHE STRING \"\")\n") + +ExternalProject_Add( + llvm + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/llvm" + GIT_REPOSITORY "https://github.com/llvm/llvm-project.git" + GIT_TAG d32170dbd5b0d54436537b6b75beaf44324e0c28 #tag llvmorg-10.0.0 + SOURCE_SUBDIR llvm + CMAKE_ARGS + -G Ninja + -C "${CMAKE_CURRENT_BINARY_DIR}/llvm_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" + -DLLVM_ABI_BREAKING_CHECKS=FORCE_OFF + -DLLVM_PARALLEL_LINK_JOBS=${_LINK_JOBS} + -DLLVM_PARALLEL_COMPILE_JOBS=${_JOBS} + -DLLVM_TARGETS_TO_BUILD=host + BUILD_COMMAND ${_BUILD_COMMAND} + INSTALL_COMMAND ${_INSTALL_COMMAND} +) diff --git a/external/programl/CMakeLists.txt b/external/programl/CMakeLists.txt new file mode 100644 index 0000000000..525a392b69 --- /dev/null +++ b/external/programl/CMakeLists.txt @@ -0,0 +1,47 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../build_tools/cmake") +find_package(Bazel REQUIRED) + +ExternalProject_Add( + programl + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/programl" + URL "https://github.com/ChrisCummins/ProGraML/archive/4f0981d7a0d27aecef3d6e918c886642b231562d.tar.gz" + URL_HASH "SHA256=c56360aade351eda1c138a594177fcb7cd2cda2a0a6c5c0d9aa62c7f856194bd" + DOWNLOAD_NO_EXTRACT FALSE + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" +) + +ExternalProject_Add_Step(programl build_programl + ALWAYS TRUE + COMMAND + "${CMAKE_COMMAND}" -E env "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" + "${Bazel_EXECUTABLE}" build + --verbose_failures + "--cxxopt=-std=c++${CMAKE_CXX_STANDARD}" + -- + //programl/graph:features + //programl/graph:program_graph_builder + //programl/graph/format:node_link_graph + //programl/ir/llvm:llvm-10 + //programl/proto:programl_cc + //programl/proto:programl + @labm8//labm8/cpp:logging + @labm8//labm8/cpp:status + @labm8//labm8/cpp:status_macros + @labm8//labm8/cpp:statusor + @labm8//labm8/cpp:string + @labm8//labm8/cpp:stringpiece + DEPENDEES update + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/programl/src/programl" +) diff --git a/external/protobuf/CMakeLists.txt b/external/protobuf/CMakeLists.txt new file mode 100644 index 0000000000..2689198672 --- /dev/null +++ b/external/protobuf/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(protobuf) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) +include(ProcessorCount) + +find_package(Git REQUIRED) + +ProcessorCount(_JOBS) +ExternalProject_Add( + protobuf + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/protobuf" + GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git" + GIT_TAG fde7cf7358ec7cd69e8db9be4f1fa6a5c431386a #v3.13.0 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" +) + +ExternalProject_Add_Step(protobuf build_protobuf + ALWAYS TRUE + COMMAND + "${CMAKE_COMMAND}" -E env "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" + "${CMAKE_COMMAND}" + -C "${CMAKE_CURRENT_BINARY_DIR}/protobuf_initial_cache.cmake" + "-DGIT_EXECUTABLE=${GIT_EXECUTABLE}" + "-DGIT_REPOSITORY_DIR=${CMAKE_CURRENT_BINARY_DIR}/protobuf/src/protobuf" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/build_protobuf.cmake" + DEPENDEES update + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/protobuf/src/protobuf" +) diff --git a/external/protobuf/build_protobuf.cmake b/external/protobuf/build_protobuf.cmake new file mode 100644 index 0000000000..35bdba3220 --- /dev/null +++ b/external/protobuf/build_protobuf.cmake @@ -0,0 +1,66 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cmake_minimum_required(VERSION 3.15) + +# Trickery to circumvent https://gitlab.kitware.com/cmake/cmake/-/issues/19703 +# Avoids rebuilding if the git was in the same state as in the previous build. + +execute_process( + COMMAND "${GIT_EXECUTABLE}" log -n 1 --pretty=format:%H + WORKING_DIRECTORY "${GIT_REPOSITORY_DIR}" + OUTPUT_VARIABLE _GIT_HASH + COMMAND_ERROR_IS_FATAL ANY + ) + +execute_process( + COMMAND "${GIT_EXECUTABLE}" diff --quiet + WORKING_DIRECTORY "${GIT_REPOSITORY_DIR}" + RESULT_VARIABLE _GIT_DIFF_RES) +if(_GIT_DIFF_RES STREQUAL 0) + set(_IS_GIT_DIRTY FALSE) +else() + set(_IS_GIT_DIRTY TRUE) +endif() + +if(NOT _IS_GIT_DIRTY AND EXISTS "${GIT_REPOSITORY_DIR}/../build_git_hash" AND + "${GIT_REPOSITORY_DIR}/../build_git_hash" IS_NEWER_THAN "${CMAKE_CURRENT_LIST_DIR}") + file(READ "${GIT_REPOSITORY_DIR}/../build_git_hash" _PREV_GIT_HASH) + if (_GIT_HASH STREQUAL _PREV_GIT_HASH) + return() + endif() +endif() + +file(REMOVE "${GIT_REPOSITORY_DIR}/../build_git_hash") + +execute_process( + COMMAND ./autogen.sh + WORKING_DIRECTORY "${GIT_REPOSITORY_DIR}" + COMMAND_ERROR_IS_FATAL ANY) + +execute_process( + COMMAND "${CMAKE_COMMAND}" + -E env + "CC=${CMAKE_C_COMPILER}" + "CXX=${CMAKE_CXX_COMPILER}" + "CFLAGS=${CMAKE_C_FLAGS} $ENV{CFLAGS}" + "CXXFLAGS=-std=c++${CMAKE_CXX_STANDARD} ${CMAKE_CXX_FLAGS} $ENV{CFLAGS}" + "LDFLAGS=-Wl,-rpath,${CMAKE_INSTALL_PREFIX}/lib ${CMAKE_STATIC_LINKER_FLAGS_INIT} ${CMAKE_SHARED_LINKER_FLAGS_INIT} ${CMAKE_EXE_LINKER_FLAGS_INIT} ${CMAKE_STATIC_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS} $ENV{LDFLAGS}" + ./configure "--prefix=${CMAKE_INSTALL_PREFIX}" + WORKING_DIRECTORY "${GIT_REPOSITORY_DIR}" + COMMAND_ERROR_IS_FATAL ANY) + +include(ProcessorCount) +ProcessorCount(_JOBS) +execute_process( + COMMAND make -j${_JOBS} install + WORKING_DIRECTORY "${GIT_REPOSITORY_DIR}" + COMMAND_ERROR_IS_FATAL ANY) + +if(_IS_GIT_DIRTY) + return() +endif() + +file(WRITE "${GIT_REPOSITORY_DIR}/../build_git_hash" "${_GIT_HASH}") diff --git a/external/subprocess/CMakeLists.txt b/external/subprocess/CMakeLists.txt new file mode 100644 index 0000000000..d5aad65b66 --- /dev/null +++ b/external/subprocess/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +project(gflags) +cmake_minimum_required(VERSION 3.15) + +include(ExternalProject) + +ExternalProject_Add( + subprocess + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/subprocess" + GIT_REPOSITORY "https://github.com/arun11299/cpp-subprocess.git" + GIT_TAG 9c624ce4e3423cce9f148bafbae56abfd6437ea0 #tag v2.0 + CMAKE_ARGS + -C "${CMAKE_CURRENT_BINARY_DIR}/subprocess_initial_cache.cmake" + "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}" + "-DCMAKE_CXX_FLAGS=-pthread" +) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000000..dc113d8b83 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,126 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + compiler_env_test + SRCS + "compiler_env_test.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + compiler_env_state_test + SRCS + "compiler_env_state_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + make_test + SRCS + "make_test.py" + DEPS + compiler_gym::compiler_gym + tests::test_main +) + +cg_py_test( + NAME + random_search_test + SRCS + "random_search_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::random_replay + compiler_gym::random_search + tests::pytest_plugins::common + tests::test_main +) + +cg_py_library( + NAME + test_main + SRCS + "test_main.py" + DEPS + compiler_gym::util::util + TESTONLY + PUBLIC +) + +cg_cc_library( + NAME + TestMacros + SRCS + "TestMacros.h" + PUBLIC +) + +cg_cc_library( + NAME + TestMain + SRCS + "TestMain.cc" + ABS_DEPS + glog::glog + GTest::GTest + TESTONLY + PUBLIC +) + +cg_py_test( + NAME + validate_test + SRCS + "validate_test.py" + DEPS + ::test_main + compiler_gym::compiler_gym +) + +cg_py_test( + NAME + validation_result_test + SRCS + "validation_result_test.py" + DEPS + ::test_main + compiler_gym::compiler_gym +) + +string(CONCAT _CMD + "\"${CMAKE_COMMAND}\" -E create_symlink" + " \"${CMAKE_CURRENT_SOURCE_DIR}/../VERSION\"" + " \"${CMAKE_CURRENT_BINARY_DIR}/../VERSION\"") +cg_genrule( + NAME version_file + SRCS "${CMAKE_CURRENT_SOURCE_DIR}/../VERSION" + OUTS "${CMAKE_CURRENT_BINARY_DIR}/../VERSION" + COMMAND "${_CMD}" +) + +cg_py_test( + NAME + version_test + SRCS + "version_test.py" + DEPS + ::test_main + ::version_file + compiler_gym::compiler_gym + tests::pytest_plugins::common +) diff --git a/tests/bin/CMakeLists.txt b/tests/bin/CMakeLists.txt new file mode 100644 index 0000000000..4e207d3733 --- /dev/null +++ b/tests/bin/CMakeLists.txt @@ -0,0 +1,51 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + datasets_bin_test + SRCS + "datasets_bin_test.py" + DEPS + compiler_gym::bin::datasets + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + manual_env_bin_test + SRCS + "manual_env_bin_test.py" + DEPS + compiler_gym::bin::manual_env + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME + service_bin_test + SRCS + "service_bin_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::bin::service + tests::test_main +) + +cg_py_test( + NAME + validate_bin_test + SRCS + "validate_bin_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::bin::validate + tests::pytest_plugins::common + tests::test_main +) diff --git a/tests/datasets/CMakeLists.txt b/tests/datasets/CMakeLists.txt new file mode 100644 index 0000000000..ce8622c3d1 --- /dev/null +++ b/tests/datasets/CMakeLists.txt @@ -0,0 +1,50 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + benchmark_test + SRCS + "benchmark_test.py" + DEPS + compiler_gym::datasets::datasets + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + dataset_test + SRCS + "dataset_test.py" + DEPS + compiler_gym::datasets::datasets + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + datasets_test + SRCS + "datasets_test.py" + DEPS + compiler_gym::datasets::datasets + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + files_dataset_test + SRCS + "files_dataset_test.py" + DEPS + compiler_gym::datasets::datasets + tests::pytest_plugins::common + tests::test_main +) diff --git a/tests/fuzzing/CMakeLists.txt b/tests/fuzzing/CMakeLists.txt new file mode 100644 index 0000000000..2201b1aa11 --- /dev/null +++ b/tests/fuzzing/CMakeLists.txt @@ -0,0 +1,104 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + llvm_cbench_validate_fuzz_test + SRCS + "llvm_cbench_validate_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::llvm + tests::test_main + LABELS + "manual" +) + +cg_py_test( + NAME + llvm_commandline_opt_equivalence_fuzz_test + SRCS + "llvm_commandline_opt_equivalence_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::pytest_plugins::random_util + tests::test_main + LABELS + "manual" +) + +cg_py_test( + NAME + llvm_deterministic_action_fuzz_test + SRCS + "llvm_deterministic_action_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::llvm + tests::test_main + LABELS + "manual" +) + +cg_py_test( + NAME + llvm_fork_env_fuzz_test + SRCS + "llvm_fork_env_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::llvm + tests::test_main + LABELS + "manual" +) + +# TODO(boian): fix when benchmarks_random_actions_test.py is present in source. +#cg_py_test( +# NAME +# benchmarks_random_actions_test +# SRCS +# "benchmarks_random_actions_test.py" +# DEPS +# compiler_gym::compiler_gym +# compiler_gym::envs::envs +# compiler_gym::third_party::autophase::autophase +# tests::pytest_plugins::llvm +# tests::test_main +# LABELS +# "manual" +#) + +cg_py_test( + NAME + llvm_trajectory_replay_fuzz_test + SRCS + "llvm_trajectory_replay_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::llvm + tests::pytest_plugins::random_util + tests::test_main + LABELS + "manual" +) + +cg_py_test( + NAME + llvm_stress_fuzz_test + SRCS + "llvm_stress_fuzz_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::llvm + tests::pytest_plugins::random_util + tests::test_main + LABELS + "manual" +) diff --git a/tests/gcc/CMakeLists.txt b/tests/gcc/CMakeLists.txt new file mode 100644 index 0000000000..da35bc57b5 --- /dev/null +++ b/tests/gcc/CMakeLists.txt @@ -0,0 +1,44 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + gcc_bin_test + SRCS + "gcc_bin_test.py" + DEPS + compiler_gym::envs::gcc::gcc + compiler_gym::service::service + tests::pytest_plugins::gcc + tests::test_main +) + +cg_py_test( + NAME + gcc_docker_test + SRCS + "gcc_docker_test.py" + DEPS + compiler_gym::envs::gcc::gcc + compiler_gym::service::service + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + gcc_env_test + SRCS + "gcc_env_test.py" + DEPS + compiler_gym::envs::gcc::gcc + compiler_gym::service::service + compiler_gym::spaces::spaces + tests::pytest_plugins::common + tests::pytest_plugins::gcc + tests::test_main +) diff --git a/tests/gcc/datasets/CMakeLists.txt b/tests/gcc/datasets/CMakeLists.txt new file mode 100644 index 0000000000..0bb4b6cb1b --- /dev/null +++ b/tests/gcc/datasets/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + anghabench_test + SRCS + "anghabench_test.py" + DEPS + compiler_gym::envs::gcc::gcc + compiler_gym::envs::gcc::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::gcc + tests::test_main +) + +cg_py_test( + NAME + csmith_test + SRCS + "csmith_test.py" + DEPS + compiler_gym::envs::gcc::gcc + compiler_gym::envs::gcc::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::gcc + tests::test_main +) diff --git a/tests/leaderboard/CMakeLists.txt b/tests/leaderboard/CMakeLists.txt new file mode 100644 index 0000000000..306bc3cfb5 --- /dev/null +++ b/tests/leaderboard/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + llvm_instcount_test + SRCS + "llvm_instcount_test.py" + DEPS + compiler_gym::leaderboard::llvm_instcount + tests::pytest_plugins::common + tests::test_main +) diff --git a/tests/llvm/CMakeLists.txt b/tests/llvm/CMakeLists.txt new file mode 100644 index 0000000000..1b6a7c40a2 --- /dev/null +++ b/tests/llvm/CMakeLists.txt @@ -0,0 +1,316 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME action_space_test + SRCS "action_space_test.py" + DEPS + compiler_gym::envs::envs + tests::test_main + tests::pytest_plugins::llvm +) + +cg_py_test( + NAME + all_actions_single_step_test + SRCS + "all_actions_single_step_test.py" + DEPS + compiler_gym::envs::envs + compiler_gym::third_party::autophase::autophase + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + all_benchmarks_init_close_test + SRCS + "all_benchmarks_init_close_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + autophase_test + SRCS + "autophase_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_filegroup( + NAME custom_benchmarks_test_files + FILES "${CMAKE_CURRENT_LIST_DIR}/invalid_ir.ll") +cg_py_test( + NAME + custom_benchmarks_test + SRCS + "custom_benchmarks_test.py" + DATA + compiler_gym::third_party::cbench::crc32 + ::custom_benchmarks_test_files + DEPS + compiler_gym::envs::envs + compiler_gym::service::proto::proto + compiler_gym::util::util + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + datasets_pickle_test + SRCS + "datasets_pickle_test.py" + DEPS + compiler_gym::datasets::datasets + compiler_gym::envs::llvm::llvm + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + download_llvm_test + SRCS + "download_llvm_test.py" + DEPS + compiler_gym::third_party::llvm::llvm + compiler_gym::util::util + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + episode_reward_test + SRCS + "episode_reward_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + fork_env_test + SRCS + "fork_env_test.py" + DATA + compiler_gym::third_party::cbench::crc32 + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +py_test( + NAME fork_regression_test + SRCS fork_regression_test.py + DEPS + compiler_gym::envs::envs + tests::test_main + tests::pytest_plugins::llvm +) + +cg_py_test( + NAME + fresh_environment_observation_reward_test + SRCS + "fresh_environment_observation_reward_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + fuzzing_regression_test + SRCS + "fuzzing_regression_test.py" + DEPS + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + gym_interface_compatability + SRCS + "gym_interface_compatability.py" + DEPS + compiler_gym::envs::llvm::llvm + tests::pytest_plugins::llvm + tests::test_main +) + +cg_filegroup( + NAME invalid_ir_test_files + FILES "${CMAKE_CURRENT_LIST_DIR}/invalid_ir.ll") +cg_py_test( + NAME + invalid_ir_test + SRCS + "invalid_ir_test.py" + DATA + invalid_ir_test_files + DEPS + compiler_gym::util::util + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + llvm_benchmarks_test + SRCS + "llvm_benchmarks_test.py" + DEPS + compiler_gym::envs::envs + compiler_gym::service::proto::proto + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + llvm_env_test + SRCS + "llvm_env_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + llvm_session_parameters_test + SRCS + "llvm_session_parameters_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + module_id_test + SRCS + "module_id_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + multiprocessing_test + SRCS + "multiprocessing_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + observation_spaces_test + SRCS + "observation_spaces_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + reward_spaces_test + SRCS + "reward_spaces_test.py" + DEPS + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME runtime_test + SRCS "runtime_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::service::connection + tests::test_main + tests::pytest_plugins::llvm +) + +cg_py_test( + NAME + service_connection_test + SRCS + "service_connection_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::envs::envs + compiler_gym::third_party::autophase::autophase + compiler_gym::util::util + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + threading_test + SRCS + "threading_test.py" + DEPS + compiler_gym::compiler_gym + tests::test_main +) + +cg_py_test( + NAME + validate_test + SRCS + "validate_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::datasets::datasets + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + validation_regression_test + SRCS + "validation_regression_test.py" + DEPS + compiler_gym::compiler_gym + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) diff --git a/tests/llvm/datasets/CMakeLists.txt b/tests/llvm/datasets/CMakeLists.txt new file mode 100644 index 0000000000..528822dadc --- /dev/null +++ b/tests/llvm/datasets/CMakeLists.txt @@ -0,0 +1,133 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + anghabench_test + SRCS + "anghabench_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + cbench_test + SRCS + "cbench_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + cbench_validate_test + SRCS + "cbench_validate_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::datasets::datasets + compiler_gym::envs::envs + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + chstone_test + SRCS + "chstone_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + clgen_test + SRCS + "clgen_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + csmith_test + SRCS + "csmith_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + github_test + SRCS + "github_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + llvm_datasets_test + SRCS + "llvm_datasets_test.py" + DEPS + compiler_gym::envs::llvm::llvm + tests::test_main +) + +cg_py_test( + NAME + llvm_stress_test + SRCS + "llvm_stress_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + poj104_test + SRCS + "poj104_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::envs::llvm::datasets::datasets + tests::pytest_plugins::common + tests::pytest_plugins::llvm + tests::test_main +) diff --git a/tests/llvm/service/CMakeLists.txt b/tests/llvm/service/CMakeLists.txt new file mode 100644 index 0000000000..548f066204 --- /dev/null +++ b/tests/llvm/service/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_cc_test( + NAME + ActionSpaceTest + SRCS + "ActionSpaceTest.cc" + DEPS + compiler_gym::envs::llvm::service::ActionSpace + tests::TestMacros + tests::TestMain + ABS_DEPS + GTest::GTest + magic_enum +) diff --git a/tests/loop_tool/CMakeLists.txt b/tests/loop_tool/CMakeLists.txt new file mode 100644 index 0000000000..97adba4d70 --- /dev/null +++ b/tests/loop_tool/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME "actions_test" + SRCS "actions_test.py" + DEPS + compiler_gym::compiler_gym + tests::test_main +) diff --git a/tests/pytest_plugins/CMakeLists.txt b/tests/pytest_plugins/CMakeLists.txt new file mode 100644 index 0000000000..a1175952b7 --- /dev/null +++ b/tests/pytest_plugins/CMakeLists.txt @@ -0,0 +1,56 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_library( + NAME + gcc + SRCS + "gcc.py" + DEPS + ::common + compiler_gym::envs::gcc::gcc + TESTONLY + PUBLIC +) + +cg_py_library( + NAME + llvm + SRCS + "llvm.py" + DATA + compiler_gym::envs::llvm::service::passes::actions_genfiles + compiler_gym::third_party::cbench::benchmarks_list + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::third_party::llvm::llvm + compiler_gym::util::util + TESTONLY + PUBLIC +) + +cg_py_library( + NAME + common + SRCS + "common.py" + DEPS + compiler_gym::util::util + TESTONLY + PUBLIC +) + +cg_py_library( + NAME + random_util + SRCS + "random_util.py" + DEPS + compiler_gym::compiler_gym + TESTONLY + PUBLIC +) diff --git a/tests/service/CMakeLists.txt b/tests/service/CMakeLists.txt new file mode 100644 index 0000000000..274fee9f5d --- /dev/null +++ b/tests/service/CMakeLists.txt @@ -0,0 +1,18 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + connection_test + SRCS + "connection_test.py" + DEPS + compiler_gym::compiler_gym + compiler_gym::envs::envs + compiler_gym::service::service + tests::test_main +) diff --git a/tests/service/proto/CMakeLists.txt b/tests/service/proto/CMakeLists.txt new file mode 100644 index 0000000000..92f2026cf2 --- /dev/null +++ b/tests/service/proto/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +py_test( + NAME py_converters_test + SRCS "py_converters_test.py" + DEPS + compiler_gym::service::proto::proto + compiler_gym::spaces::spaces + tests::test_main +) diff --git a/tests/service/runtime/CMakeLists.txt b/tests/service/runtime/CMakeLists.txt new file mode 100644 index 0000000000..b18ef2a40e --- /dev/null +++ b/tests/service/runtime/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + benchmark_cache_test + SRCS + "benchmark_cache_test.py" + DEPS + compiler_gym::service::proto::proto + compiler_gym::service::runtime::benchmark_cache + tests::test_main +) + +cg_cc_test( + NAME + BenchmarkCacheTest + SRCS + "BenchmarkCacheTest.cc" + DEPS + compiler_gym::service::proto::compiler_gym_service_cc + compiler_gym::service::runtime::BenchmarkCache + tests::TestMain + ABS_DEPS + GTest::GTest +) diff --git a/tests/spaces/CMakeLists.txt b/tests/spaces/CMakeLists.txt new file mode 100644 index 0000000000..d80905b624 --- /dev/null +++ b/tests/spaces/CMakeLists.txt @@ -0,0 +1,46 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + commandline_test + SRCS + "commandline_test.py" + DEPS + compiler_gym::spaces::spaces + tests::test_main +) + +cg_py_test( + NAME + named_discrete_test + SRCS + "named_discrete_test.py" + DEPS + compiler_gym::spaces::spaces + tests::test_main +) + +cg_py_test( + NAME + scalar_test + SRCS + "scalar_test.py" + DEPS + compiler_gym::spaces::spaces + tests::test_main +) + +cg_py_test( + NAME + sequence_test + SRCS + "sequence_test.py" + DEPS + compiler_gym::spaces::spaces + tests::test_main +) diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt new file mode 100644 index 0000000000..92a6074425 --- /dev/null +++ b/tests/util/CMakeLists.txt @@ -0,0 +1,167 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + capture_output_test + SRCS + "capture_output_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME + debug_util_test + SRCS + "debug_util_test.py" + DEPS + compiler_gym::util::util + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + download_test + SRCS + "download_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME executor_test + SRCS executor_test.py + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_cc_test( + NAME + EnumUtilTest + SRCS + "EnumUtilTest.cc" + DEPS + compiler_gym::util::EnumUtil + tests::TestMacros + tests::TestMain + ABS_DEPS + GTest::GTest +) + +cg_py_test( + NAME + filesystem_test + SRCS + "filesystem_test.py" + DEPS + compiler_gym::util::util + tests::pytest_plugins::common + tests::test_main +) + +cg_py_test( + NAME + minimize_trajectory_test + SRCS + "minimize_trajectory_test.py" + DEPS + compiler_gym::util::util + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + parallelization_test + SRCS + "parallelization_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME runfiles_path_test + SRCS "runfiles_path_test.py" + DEPS + compiler_gym::util::util + tests::test_main + tests::pytest_plugins::common +) + +cg_py_test( + NAME shell_format_test + SRCS shell_format_test.py + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME + statistics_test + SRCS + "statistics_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_cc_test( + NAME + StrLenConstexprTest + SRCS + "StrLenConstexprTest.cc" + DEPS + compiler_gym::util::StrLenConstexpr + tests::TestMain +) + +cg_cc_test( + NAME + SubprocessTest + SRCS + "SubprocessTest.cc" + DEPS + compiler_gym::util::Subprocess + tests::TestMain +) + +cg_py_test( + NAME + temporary_working_directory_test + SRCS + "temporary_working_directory_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME + timer_test + SRCS + "timer_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) + +cg_py_test( + NAME + truncate_test + SRCS + "truncate_test.py" + DEPS + compiler_gym::util::util + tests::test_main +) diff --git a/tests/version_test.py b/tests/version_test.py index b5af4aa485..4533770ef6 100644 --- a/tests/version_test.py +++ b/tests/version_test.py @@ -2,24 +2,14 @@ # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -import os - import pkg_resources -import pytest import compiler_gym from compiler_gym.util.runfiles_path import runfiles_path from packaging import version -from tests.pytest_plugins.common import bazel_only +from tests.pytest_plugins.common import bazel_only, install_test_only from tests.test_main import main -# Marker to skip a test if running under bazel. -# This uses $TEST_WORKSPACE, set by the bazel test runner. -# See: https://docs.bazel.build/versions/master/test-encyclopedia.html#initial-conditions -install_test = pytest.mark.skipif( - bool(os.environ.get("TEST_WORKSPACE")), reason="Install test" -) - def test_version_dunder(): assert isinstance(compiler_gym.__version__, str) @@ -29,7 +19,7 @@ def test_version_dunder_format(): version.parse(compiler_gym.__version__) -@install_test +@install_test_only def test_setuptools_version(): version = pkg_resources.require("compiler_gym")[0].version assert version == compiler_gym.__version__ diff --git a/tests/views/CMakeLists.txt b/tests/views/CMakeLists.txt new file mode 100644 index 0000000000..623239c5bc --- /dev/null +++ b/tests/views/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + observation_test + SRCS + "observation_test.py" + DEPS + compiler_gym::service::proto::proto + compiler_gym::views::views + tests::test_main +) + +cg_py_test( + NAME + reward_test + SRCS + "reward_test.py" + DEPS + compiler_gym::views::views + tests::test_main +) diff --git a/tests/wrappers/CMakeLists.txt b/tests/wrappers/CMakeLists.txt new file mode 100644 index 0000000000..3b7e2303e5 --- /dev/null +++ b/tests/wrappers/CMakeLists.txt @@ -0,0 +1,60 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cg_add_all_subdirs() + +cg_py_test( + NAME + commandline_wrappers_test + SRCS + "commandline_wrappers_test.py" + DEPS + compiler_gym::wrappers::wrappers + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + core_wrappers_test + SRCS + "core_wrappers_test.py" + DEPS + compiler_gym::wrappers::wrappers + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME + datasets_wrappers_test + SRCS + "datasets_wrappers_test.py" + DEPS + compiler_gym::wrappers::wrappers + tests::pytest_plugins::llvm + tests::test_main +) + +cg_py_test( + NAME llvm_test + SRCS "llvm_test.py" + DEPS + compiler_gym::envs::llvm::llvm + compiler_gym::wrappers::wrappers + tests::test_main + tests::pytest_plugins::llvm +) + +cg_py_test( + NAME + time_limit_wrappers_test + SRCS + "time_limit_wrappers_test.py" + DEPS + compiler_gym::wrappers::wrappers + tests::pytest_plugins::llvm + tests::test_main +)