Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Building with CMake #498

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/actions/install-cmake-build-dependencies/action.yaml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
ChrisCummins marked this conversation as resolved.
Show resolved Hide resolved

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()
80 changes: 75 additions & 5 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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/)
Expand All @@ -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
Expand All @@ -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_<dependency>_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
Comment on lines +130 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-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
-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_EXE_LINKER_FLAGS_INIT="-fuse-ld=lld" -DCMAKE_MODULE_LINKER_FLAGS_INIT="-fuse-ld=lld" -DCMAKE_SHARED_LINKER_FLAGS_INIT="-fuse-ld=lld" \

On my shell (zsh 5.8), anything after the escaping \ is invalid and causes a syntax error:

$ cmake -GNinja \
  -DCMAKE_C_COMPILER=clang-9 \
  -DCMAKE_CXX_COMPILER=clang++-9 \
  -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
  -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 \
  -S "$SOURCE_DIR" \
  -B "$BUILD_DIR"
zsh: no matches found:  #
zsh: no matches found:  #
zsh: command not found: -DPython3_FIND_VIRTUALENV=FIRST

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reorganized the instructions and this problem is not present there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this optional arguments are still in the base command though. Tbh I think it would be fine to simply update the lld to lld-9 and then include them as part of the base command, since the lld-9 package is part of the required dependencies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated to use lld-9 explicitly. I think on your system, lld-9 is not the default lld.

-DPython3_FIND_VIRTUALENV=FIRST \
-S "<path to source directory>" \
-B "<path to build directory>"

cmake --build "<path to build directory>"
```
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
```
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===
Expand Down
53 changes: 53 additions & 0 deletions build_tools/cmake/FindBazel.cmake
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this file (and all other *.cmake files), is it your own code? If so it needs the CompilerGym license header. If it's taken from another project, does it need a license header + attribution for that project?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some parts of the code come from IREE's Bazel to CMake autoconversion tool. That is under Apache 2.0 license and it needs attribution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, in that case I think that the *.cmake files should have IREE's license header added

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
)
42 changes: 42 additions & 0 deletions build_tools/cmake/FindClog.cmake
Original file line number Diff line number Diff line change
@@ -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)
Loading