From 2a6d39124aa8834cef7f09e3b46cd69c51694643 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 23 Oct 2024 08:29:57 +0200 Subject: [PATCH] build: add recipes for installing LLVM --- deps/test/llvm/test_install.c | 21 +++ tools/make/common.mk | 12 ++ tools/make/lib/install/Makefile | 1 + tools/make/lib/install/README.md | 33 +++++ tools/make/lib/install/llvm.mk | 247 +++++++++++++++++++++++++++++++ 5 files changed, 314 insertions(+) create mode 100644 deps/test/llvm/test_install.c create mode 100644 tools/make/lib/install/llvm.mk diff --git a/deps/test/llvm/test_install.c b/deps/test/llvm/test_install.c new file mode 100644 index 000000000000..3979a476b61d --- /dev/null +++ b/deps/test/llvm/test_install.c @@ -0,0 +1,21 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +double add( double x, double y ) { + return x + y; +} diff --git a/tools/make/common.mk b/tools/make/common.mk index 935af1dfab76..3ebdeeae02d2 100644 --- a/tools/make/common.mk +++ b/tools/make/common.mk @@ -490,6 +490,18 @@ ifeq (, $(BLAS_DIR)) endif endif +# Define the output path when building LLVM: +DEPS_LLVM_BUILD_OUT ?= $(DEPS_BUILD_DIR)/llvm + +# Define the LLVM version: +DEPS_LLVM_VERSION ?= + +# Define the path to clang compiler: +DEPS_LLVM_CLANG ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/clang + +# Define the path to LLVM static compiler: +DEPS_LLVM_LLC ?= $(DEPS_LLVM_BUILD_OUT)/build/bin/llc + # Define the output path when building the Emscripten SDK: DEPS_EMSDK_BUILD_OUT ?= $(DEPS_BUILD_DIR)/emsdk diff --git a/tools/make/lib/install/Makefile b/tools/make/lib/install/Makefile index 309a63391766..0ea08bfccbf2 100644 --- a/tools/make/lib/install/Makefile +++ b/tools/make/lib/install/Makefile @@ -39,6 +39,7 @@ include $(TOOLS_MAKE_LIB_DIR)/install/cephes.mk include $(TOOLS_MAKE_LIB_DIR)/install/cppcheck.mk include $(TOOLS_MAKE_LIB_DIR)/install/electron.mk include $(TOOLS_MAKE_LIB_DIR)/install/emsdk.mk +include $(TOOLS_MAKE_LIB_DIR)/install/llvm.mk include $(TOOLS_MAKE_LIB_DIR)/install/node.mk include $(TOOLS_MAKE_LIB_DIR)/install/openblas.mk include $(TOOLS_MAKE_LIB_DIR)/install/python_deps.mk diff --git a/tools/make/lib/install/README.md b/tools/make/lib/install/README.md index 84b39c07a41b..ecb5579ca0ce 100644 --- a/tools/make/lib/install/README.md +++ b/tools/make/lib/install/README.md @@ -37,6 +37,7 @@ This directory contains [`make`][make] rules for running the project's installat - [Cppcheck](#cppcheck) - [Electron](#electron) - [Emscripten SDK](#emscripten-sdk) + - [LLVM](#llvm) - [OpenBLAS](#openblas) - [Python](#python) - [R](#r) @@ -419,6 +420,36 @@ $ make clean-deps-emscripten-tests * * * + + +### LLVM + +#### install-deps-llvm + +Installs [LLVM][llvm]. + +```bash +$ make install-deps-llvm +``` + +#### clean-deps-llvm + +Removes an installed [LLVM][llvm] distribution. + +```bash +$ make clean-deps-llvm +``` + +#### clean-deps-llvm-tests + +Removes [LLVM][llvm] installation tests. + +```bash +$ make clean-deps-llvm-tests +``` + +* * * + ### OpenBLAS @@ -595,6 +626,8 @@ $ make clean-deps-wabt-tests [emscripten-sdk]: https://github.com/emscripten-core/emsdk +[llvm]: https://llvm.org + [node-js]: https://nodejs.org/en/ [node-js-add-ons]: https://nodejs.org/api/addons.html diff --git a/tools/make/lib/install/llvm.mk b/tools/make/lib/install/llvm.mk new file mode 100644 index 000000000000..bfae2cad68fb --- /dev/null +++ b/tools/make/lib/install/llvm.mk @@ -0,0 +1,247 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2017 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +# Define the path to an executable for checking CMake: +DEPS_CHECK_CMAKE ?= $(TOOLS_DIR)/scripts/check_cmake + +# Define the path to an executable for checking git: +DEPS_CHECK_GIT ?= $(TOOLS_DIR)/scripts/check_git + +# Define the path to an executable for checking Python: +DEPS_CHECK_PYTHON ?= $(TOOLS_DIR)/scripts/check_python + +# Define the download URL: +DEPS_LLVM_URL ?= https://github.com/llvm/llvm-project.git + +# Determine the basename for the download: +deps_llvm_basename := llvm + +# Define the path to the file containing a checksum to verify a download: +DEPS_LLVM_CHECKSUM ?= $(shell $(CAT) $(DEPS_CHECKSUMS_DIR)/$(subst .,_,$(deps_llvm_basename))/sha256) + +# Define the output path when downloading: +DEPS_LLVM_DOWNLOAD_OUT ?= $(DEPS_TMP_DIR)/$(deps_llvm_basename) + +# Define the output path after extracting: +deps_llvm_extract_out := $(DEPS_BUILD_DIR)/llvm_extracted + +# Define the path to the directory containing tests: +DEPS_LLVM_TEST_DIR ?= $(DEPS_DIR)/test/llvm + +# Define the output directory path for compiled tests: +DEPS_LLVM_TEST_OUT ?= $(DEPS_LLVM_TEST_DIR)/build + +# Define the path to a test file for checking an installation: +DEPS_LLVM_TEST_INSTALL ?= $(DEPS_LLVM_TEST_DIR)/test_install.c + +# Define output paths for a compiled test file: +DEPS_LLVM_TEST_INSTALL_WASM_OUT ?= $(DEPS_LLVM_TEST_OUT)/test.wasm + + +# RULES # + +#/ +# Downloads LLVM. +# +# @private +#/ +$(DEPS_LLVM_DOWNLOAD_OUT): | $(DEPS_TMP_DIR) + $(QUIET) echo 'Downloading LLVM...' >&2 + $(QUIET) $(GIT) clone --depth=1 $(DEPS_LLVM_URL) $(DEPS_LLVM_DOWNLOAD_OUT) + +#/ +# Extracts an LLVM download. +# +# @private +#/ +$(DEPS_LLVM_BUILD_OUT): | $(DEPS_BUILD_DIR) $(DEPS_LLVM_DOWNLOAD_OUT) + $(QUIET) echo 'Extracting LLVM...' >&2 + $(QUIET) $(CP) -a $(DEPS_LLVM_DOWNLOAD_OUT) $(deps_llvm_extract_out) + $(QUIET) mv $(deps_llvm_extract_out) $(DEPS_LLVM_BUILD_OUT) + +#/ +# Creates a directory for storing compiled LLVM tests. +# +# @private +#/ +$(DEPS_LLVM_TEST_OUT): + $(QUIET) $(MKDIR_RECURSIVE) $(DEPS_LLVM_TEST_OUT) + +#/ +# Compiles a WebAssembly test file for testing an LLVM installation. +# +# @private +#/ +$(DEPS_LLVM_TEST_INSTALL_WASM_OUT): $(DEPS_LLVM_BUILD_OUT) $(DEPS_LLVM_TEST_OUT) + $(QUIET) $(DEPS_LLVM_CLANG) $(DEPS_LLVM_TEST_INSTALL) \ + --target=wasm32 \ + --no-standard-libraries \ + -Wl,--export-all \ + -Wl,--no-entry \ + -o $(DEPS_LLVM_TEST_INSTALL_WASM_OUT) + +#/ +# Downloads LLVM. +# +# @private +# +# @example +# make deps-download-llvm +#/ +deps-download-llvm: $(DEPS_LLVM_DOWNLOAD_OUT) + +.PHONY: deps-download-llvm + +#/ +# Verifies an LLVM download. +# +# @private +# +# @example +# make deps-verify-llvm +#/ +deps-verify-llvm: deps-download-llvm + $(QUIET) echo 'Verifying download...' >&2 + $(QUIET) echo 'Nothing to verify.' >&2 + +.PHONY: deps-verify-llvm + +#/ +# Extracts an LLVM download. +# +# @private +# +# @example +# make deps-extract-llvm +#/ +deps-extract-llvm: $(DEPS_LLVM_BUILD_OUT) + +.PHONY: deps-extract-llvm + +#/ +# Checks a host system for LLVM installation prerequisites. +# +# @private +# +# @example +# make deps-prerequisites-llvm +#/ +deps-prerequisites-llvm: + $(QUIET) $(DEPS_CHECK_CMAKE) + $(QUIET) $(DEPS_CHECK_GIT) + $(QUIET) $(DEPS_CHECK_PYTHON) + +.PHONY: deps-prerequisites-llvm + +#/ +# Installs LLVM. +# +# @private +# +# @example +# make deps-install-llvm +#/ +deps-install-llvm: $(DEPS_LLVM_BUILD_OUT) deps-prerequisites-llvm + $(QUIET) cd $(DEPS_LLVM_BUILD_OUT) && $(MKDIR_RECURSIVE) build + $(QUIET) cd $(DEPS_LLVM_BUILD_OUT)/build && $(CMAKE) -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm + $(QUIET) cd $(DEPS_LLVM_BUILD_OUT)/build && $(MAKE) + +.PHONY: deps-install-llvm + +#/ +# Updates an installed LLVM. +# +# @private +# +# @example +# make deps-update-llvm +#/ +deps-update-llvm: deps-install-llvm deps-test-llvm + +.PHONY: deps-update-llvm + +# +# Tests an LLVM installation. +# +# @private +# +# @example +# make deps-test-llvm +#/ +deps-test-llvm: $(DEPS_LLVM_TEST_INSTALL_WASM_OUT) deps-test-llvm-wasm + $(QUIET) echo '' >&2 + $(QUIET) echo 'LLVM info...' >&2 + $(QUIET) echo '' >&2 + $(QUIET) $(DEPS_LLVM_CLANG) --help >&2 + $(QUIET) echo '' >&2 + $(QUIET) cd $(DEPS_LLVM_BUILD_OUT)/build && $(MAKE) check-clang + $(QUIET) echo 'Success.' >&2 + +.PHONY: deps-test-llvm + +#/ +# Tests an LLVM installation for generating WebAssembly. +# +# @private +# +# @example +# make deps-test-llvm-wasm +#/ +deps-test-llvm-wasm: $(DEPS_LLVM_TEST_INSTALL_WASM_OUT) + $(QUIET) echo 'Running wasm tests...' >&2 + $(QUIET) echo '' >&2 + $(QUIET) echo 'TODO: make test more robust' >&2 + $(QUIET) test -f $(DEPS_LLVM_TEST_INSTALL_WASM_OUT) >&2 + $(QUIET) echo '' >&2 + $(QUIET) echo 'Success.' >&2 + +.PHONY: deps-test-llvm-wasm + +#/ +# Installs LLVM. +# +# @example +# make install-deps-llvm +#/ +install-deps-llvm: deps-download-llvm deps-verify-llvm deps-extract-llvm deps-install-llvm deps-test-llvm + +.PHONY: install-deps-llvm + +#/ +# Removes an LLVM installation (but does not remove an LLVM download if one exists). +# +# @example +# make clean-deps-llvm +#/ +clean-deps-llvm: clean-deps-llvm-tests + $(QUIET) $(DELETE) $(DELETE_FLAGS) $(DEPS_LLVM_BUILD_OUT) + +.PHONY: clean-deps-llvm + +#/ +# Removes LLVM installation tests. +# +# @example +# make clean-deps-llvm-tests +#/ +clean-deps-llvm-tests: + $(QUIET) $(DELETE) $(DELETE_FLAGS) $(DEPS_LLVM_TEST_OUT) + +.PHONY: clean-deps-llvm-tests