diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..be16bd1 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,17 @@ +common --announce_rc + +build --cxxopt="-std=c++17" +build --cxxopt="-gz" --cxxopt="-gdwarf-4" +build --cxxopt="-Wall" --cxxopt="-Wextra" --cxxopt="-Werror" +build --cxxopt="-Wno-cast-function-type-mismatch" +build --cxxopt="-Wno-unused-function" --cxxopt="-Wno-unused-parameter" # protobuf require +build --cxxopt="-Wno-deprecated-declarations" # cuda require +build --linkopt="-Wl,--export-dynamic" --linkopt="-Wl,--build-id" +build --linkopt="-flto=thin" +build --compilation_mode=opt --copt=-g --copt=-O3 + +# Use --config=clang to build with clang instead of gcc and nvcc. +build:clang --repo_env=CC=clang + +test --test_output=all +test --cxxopt="-Wno-deprecated-declarations" diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..278cfbd --- /dev/null +++ b/.clang-format @@ -0,0 +1,26 @@ +--- +BasedOnStyle: Google +--- +Language: Cpp +Cpp11BracedListStyle: true +Standard: Cpp11 +AllowShortFunctionsOnASingleLine: None +AllowShortLambdasOnASingleLine: None +DerivePointerAlignment: false +PointerAlignment: Left +CommentPragmas: "^ NOLINT" +ColumnLimit: 100 +IncludeCategories: + - Regex: '^<(.*)(\.h>)$' + Priority: 1 + - Regex: "^<" + Priority: 2 + - Regex: '^"([a-zA-Z_]*)(\.h")$' + Priority: 3 + - Regex: '^"(gtest|gflags|glog|google|boost)/' + Priority: 5 + - Regex: '^"(proto)/' + Priority: 7 + - Regex: ".*" + Priority: 8 +--- diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..be6d6aa --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,128 @@ +--- +Checks: '-*, + clang-analyzer-core.*, + clang-analyzer-cplusplus.*, + modernize-deprecated-headers, + modernize-make-shared, + modernize-make-unique, + modernize-redundant-void-arg, + modernize-use-bool-literals, + modernize-use-emplace, + modernize-use-equals-default, + modernize-use-nullptr, + modernize-use-override, + google-build-explicit-make-pair, + google-build-namespaces, + google-build-using-namespace, + google-default-arguments, + google-explicit-constructor, + google-global-names-in-headers, + google-readability-avoid-underscore-in-googletest-name, + google-readability-casting, + google-runtime-int, + readability-braces-around-statements, + readability-container-size-empty, + readability-identifier-naming.GlobalConstant, + readability-identifier-naming.ConstantMember, + readability-identifier-naming.ClassCase, + readability-identifier-naming.StructCase, + readability-identifier-naming.TypedefCase, + readability-identifier-naming.EnumCase, + readability-make-member-function-const, + readability-non-const-parameter, + readability-redundant-smartptr-get, + cert-dcl21-cpp, + bugprone-undelegated-constructor, + bugprone-macro-parentheses, + bugprone-macro-repeated-side-effects, + bugprone-forward-declaration-namespace, + bugprone-bool-pointer-implicit-conversion, + bugprone-misplaced-widening-cast, + misc-unconventional-assign-operator, + misc-unused-parameters' +WarningsAsErrors: '' +HeaderFilterRegex: '' +CheckOptions: + # Modernize + - key: modernize-deprecated-headers + value: 'true' + - key: modernize-make-shared + value: 'true' + - key: modernize-make-unique + value: 'true' + - key: modernize-redundant-void-arg + value: 'true' + - key: modernize-use-bool-literals + value: 'false' + - key: modernize-use-emplace + value: 'true' + - key: modernize-use-equals-default + value: 'true' + - key: modernize-use-nullptr + value: 'true' + - key: modernize-use-override + value: 'true' + # Google + - key: google-build-explicit-make-pair + value: 'true' + - key: google-build-namespaces + value: 'true' + - key: google-build-using-namespace + value: 'true' + - key: google-default-arguments + value: 'true' + - key: google-explicit-constructor + value: 'true' + - key: google-global-names-in-headers + value: 'true' + - key: google-readability-avoid-underscore-in-googletest-name + value: 'true' + - key: google-readability-casting + value: 'true' + - key: google-runtime-int + value: 'true' + # Readability + - key: readability-braces-around-statements + value: 'true' + - key: readability-container-size-empty + value: 'true' + - key: readability-identifier-naming.GlobalConstant + value: 'kCamelCase' + - key: readability-identifier-naming.ConstantMember + value: 'kCamelCase' + - key: readability-identifier-naming.ClassCase + value: 'CamelCase' + - key: readability-identifier-naming.StructCase + value: 'CamelCase' + - key: readability-identifier-naming.TypedefCase + value: 'CamelCase' + - key: readability-identifier-naming.EnumCase + value: 'CamelCase' + - key: readability-make-member-function-const + value: 'true' + - key: readability-non-const-parameter + value: 'true' + - key: readability-redundant-smartptr-get + value: 'true' + # CERT + - key: cert-dcl21-cpp + value: 'true' + # Bugprone + - key: bugprone-undelegated-constructor + value: 'true' + - key: bugprone-macro-parentheses + value: 'true' + - key: bugprone-macro-repeated-side-effects + value: 'true' + - key: bugprone-forward-declaration-namespace + value: 'true' + - key: bugprone-bool-pointer-implicit-conversion + value: 'true' + - key: bugprone-misplaced-widening-cast + value: 'true' + # CppCoreGuidelines + # Miscellaneous + - key: misc-unconventional-assign-operator + value: 'true' + - key: misc-unused-parameters + value: 'true' diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..20fdbeb --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,2 @@ +load("@rules_pkg//pkg:tar.bzl", "pkg_tar") +load("@rules_pkg//pkg:mappings.bzl", "pkg_files") diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..41ce96f --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,53 @@ +# Hedron's Compile Commands Extractor for Bazel +# https://github.com/hedronvision/bazel-compile-commands-extractor +bazel_dep(name = "hedron_compile_commands", dev_dependency = True) +git_override( + module_name = "hedron_compile_commands", + remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git", + commit = "1e08f8e0507b6b6b1f4416a9a22cf5c28beaba93", + # Replace the commit hash (above) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main). + # Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README). +) + +bazel_dep(name = "rules_pkg", version = "0.9.1") +bazel_dep(name = 'rules_cc', version = '0.0.12') +bazel_dep(name = 'rules_proto', version = '6.0.2') +bazel_dep(name = "nlohmann_json", version = "3.11.3.bcr.1") +bazel_dep(name = "googletest", version = "1.12.1", repo_name = "com_google_googletest") +bazel_dep(name = "gflags", version = "2.2.2", repo_name = "com_github_gflags_gflags") +bazel_dep(name = "glog", version = "0.7.1", repo_name = "com_github_glog_glog") +bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf") +bazel_dep(name = "llvm") + +# Boost +# Famous C++ library that has given rise to many new additions to the C++ Standard Library +# Makes @boost available for use: For example, add `@boost//:algorithm` to your deps. +# For more, see https://github.com/nelhage/rules_boost and https://www.boost.org +bazel_dep(name = "rules_boost", repo_name = "com_github_nelhage_rules_boost") +archive_override( + module_name = "rules_boost", + urls = "https://github.com/nelhage/rules_boost/archive/refs/heads/master.tar.gz", + strip_prefix = "rules_boost-master", + # It is recommended to edit the above URL and the below sha256 to point to a specific version of this repository. + # integrity = "sha256-...", +) + +non_module_boost_repositories = use_extension("@com_github_nelhage_rules_boost//:boost/repositories.bzl", "non_module_dependencies") +use_repo( + non_module_boost_repositories, + "boost", +) + +# Python rules for Bazel +bazel_dep(name = "rules_python", version = "0.38.0") +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + configure_coverage_tool = False, + ignore_root_user_error = True, + python_version = "3.10", +) + +local_path_override( + module_name = "llvm", + path = "llvm", +) \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 6ffe24c..d02c560 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -88,8 +88,8 @@ RUN apt update && \ # install zsh & fix ssh env WORKDIR /tmp RUN apt update && apt install zsh openssh-client openssh-server -y && \ - echo "export \$(cat /proc/1/environ |tr '\\0' '\\n' | xargs)" >> /etc/profile && \ - echo "export \$(cat /proc/1/environ |tr '\\0' '\\n' | xargs)" >> /etc/zsh/zprofile && \ + echo "export \$(cat /proc/1/environ |tr '\\\0' '\\\n' | xargs)" >> /etc/profile && \ + echo "export \$(cat /proc/1/environ |tr '\\\0' '\\\n' | xargs)" >> /etc/zsh/zprofile && \ rm -rf ~/.oh-my-zsh || true && \ curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -o install.sh && \ chmod +x install.sh && \ @@ -121,6 +121,10 @@ RUN apt update && \ FROM base as final-build USER root WORKDIR /workspace +ENV CC "clang" +ENV CXX "clang++" +ENV CPP "clang++ -E" +ENV LD "clang++" ENV PATH $PATH:/usr/local/cuda/bin:/usr/src/tensorrt/bin ENV LD_LIBRARY_PATH $LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/targets/x86_64-linux/lib:/usr/lib/llvm-20/lib ENV LIBRARY_PATH $LIBRARY_PATH::/usr/local/lib:/usr/local/cuda/lib64:/usr/local/cuda/targets/x86_64-linux/lib:/usr/lib/llvm-20/lib @@ -130,6 +134,10 @@ ENV CPLUS_INCLUDE_PATH $CPLUS_INCLUDE_PATH:/usr/local/cuda/include://usr/include FROM dev-stage as final-dev USER root WORKDIR /workspace +ENV CC "clang" +ENV CXX "clang++" +ENV CPP "clang++ -E" +ENV LD "clang++" ENV PATH $PATH:/usr/local/cuda/bin:/usr/src/tensorrt/bin ENV LD_LIBRARY_PATH $LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/targets/x86_64-linux/lib:/usr/lib/llvm-20/lib ENV LIBRARY_PATH $LIBRARY_PATH::/usr/local/lib:/usr/local/cuda/lib64:/usr/local/cuda/targets/x86_64-linux/lib:/usr/lib/llvm-20/lib diff --git a/llvm/BUILD.bazel b/llvm/BUILD.bazel new file mode 100644 index 0000000..ea0132c --- /dev/null +++ b/llvm/BUILD.bazel @@ -0,0 +1,48 @@ +cc_library( + name = "clang_libs", + linkopts = [ + "-L/usr/lib/llvm-20/lib", + "-Wl,--start-group", + "-lclangAPINotes", + "-lclangARCMigrate", + "-lclangAST", + "-lclangASTMatchers", + "-lclangAnalysis", + "-lclangBasic", + "-lclangCodeGen", + "-lclangCrossTU", + "-lclangDependencyScanning", + "-lclangDirectoryWatcher", + "-lclangDriver", + "-lclangDynamicASTMatchers", + "-lclangEdit", + "-lclangFormat", + "-lclangFrontend", + "-lclangFrontendTool", + "-lclangHandleCXX", + "-lclangHandleLLVM", + "-lclangIndex", + "-lclangInterpreter", + "-lclangLex", + "-lclangTooling", + "-lclangToolingCore", + "-lclangToolingInclusions", + "-lclangToolingRefactoring", + "-lclangToolingSyntax", + "-lclangParse", + "-lclangRewrite", + "-lclangRewriteFrontend", + "-lclangSema", + "-lclangSerialization", + "-lclangSupport", + "-lLLVM-20", + "-Wl,--end-group", + "-ldl", + "-lpthread", + "-lrt", + "-ltinfo", + "-lm", + "-lncurses", + ], + visibility = ["//visibility:public"], +) diff --git a/llvm/MODULE.bazel b/llvm/MODULE.bazel new file mode 100644 index 0000000..14a2c2b --- /dev/null +++ b/llvm/MODULE.bazel @@ -0,0 +1,3 @@ +module( + name = "llvm", +) diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..02a2031 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e + +# Bazel build +bazel build $(bazel query //...) --action_env=LIBRARY_PATH=$LIBRARY_PATH diff --git a/scripts/update_compile_commands.sh b/scripts/update_compile_commands.sh new file mode 100755 index 0000000..3ef96e0 --- /dev/null +++ b/scripts/update_compile_commands.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -e + +# This script is used to update the compile_commands.json file in the root of the project. +# This file is used by clang-tidy and other tools to get the compile commands for each file. +# This script should be run after building the project with bazel + +info() { + echo -e "\033[0;34m[INFO]\033[0m $1" +} + +error() { + echo -e "\033[0;31m[ERROR]\033[0m $1" +} + +warning() { + echo -e "\033[0;33m[WARNING]\033[0m $1" +} + +ok() { + echo -e "\033[0;32m[OK]\033[0m $1" +} + + +info "Updating compile_commands.json file\n" + +bazel build $(bazel query //...) --action_env=LIBRARY_PATH=$LIBRARY_PATH || true +bazel run @hedron_compile_commands//:refresh_all + +ok "Successfully updated compile_commands.json file"