diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a36591a --- /dev/null +++ b/.clang-format @@ -0,0 +1,228 @@ +--- +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +--- +Language: Cpp +# Do not offset access specifiers (public, protected, private) +AccessModifierOffset: -4 +# Align parameters after opening brackets (round, angle, square) +AlignAfterOpenBracket: Align +# Do not align consecutive assignments +AlignConsecutiveAssignments: false +# Do not align consecutive declarations +AlignConsecutiveDeclarations: false +# Align consecutive macros +AlignConsecutiveMacros: true +# Align escaped newlines to the left +AlignEscapedNewlines: Left +# Align operands of binary and ternary expressions horizontally +AlignOperands: true +# Align trailing comments +AlignTrailingComments: true +# Allow all function arguments on the next line +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +# Do not allow short blocks (e.g., while blocks) on a single line +AllowShortBlocksOnASingleLine: false +# Do not allow short case labels on a single line +AllowShortCaseLabelsOnASingleLine: false +# Do not allow non-empty functions defined outside of a class on a single line +AllowShortFunctionsOnASingleLine: Inline +# Do not allow short if statements on a single line +AllowShortIfStatementsOnASingleLine: false +# Do not allow short loop blocks on a single line +AllowShortLoopsOnASingleLine: false +# Do not break after the return type in function definitions, unless it exceeds the column limit +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +# Do not break before multiline strings +AlwaysBreakBeforeMultilineStrings: false +# Always break after template declarations +AlwaysBreakTemplateDeclarations: Yes +# Do not require each function argument to be on a separate line +BinPackArguments: false +# Do not require each function parameter to be on a separate line +BinPackParameters: false +# Configure brace wrapping style +BraceWrapping: + # Do not break after case labels + AfterCaseLabel: false + # Do not break after class definitions + AfterClass: false + # Do not break after control statements + AfterControlStatement: false + # Do not break after enum definitions + AfterEnum: false + # Do not break after function definitions + AfterFunction: false + # Do not break after namespace definitions + AfterNamespace: false + # Do not break after struct definitions + AfterStruct: false + # Do not break after union definitions + AfterUnion: false + # Do not break after extern blocks + AfterExternBlock: false + # Break before catch statements + BeforeCatch: true + # Break before else statements + BeforeElse: true + # Do not break before lambda bodies + BeforeLambdaBody: false + # Break before while statements + BeforeWhile: true + # Do not indent braces + IndentBraces: false + # Allow empty function bodies on a single line + SplitEmptyFunction: false + # Allow empty statements on a single line + SplitEmptyRecord: false + # Allow empty namespaces on a single line + SplitEmptyNamespace: false +# Break before binary operators, except for assignment operators +BreakBeforeBinaryOperators: NonAssignment +# Customize break before braces in BraceWrapping +BreakBeforeBraces: Custom +# Do not break after commas in inheritance lists +BreakBeforeInheritanceComma: false +# Break before colons in inheritance lists, but not before commas +BreakInheritanceList: BeforeColon +# Break before ternary operators +BreakBeforeTernaryOperators: true +# Do not break after commas in constructor initializer lists +BreakConstructorInitializersBeforeComma: false +# Break before colons in constructor initializer lists, but not before commas +BreakConstructorInitializers: BeforeColon +# Allow breaking string literals +BreakStringLiterals: true +# Column limit for each line +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +# Do not allow different namespaces to be defined on the same line +CompactNamespaces: false +# Constructor initializer lists should either be all on one line or one per line +ConstructorInitializerAllOnOneLineOrOnePerLine: true +# Indent constructor initializer lists by 4 spaces +ConstructorInitializerIndentWidth: 4 +# Indent continuation lines by 4 spaces +ContinuationIndentWidth: 4 +# No spaces around variable initialization lists +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +# Automatically add comments at the end of namespaces +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: ^ + Priority: 2 + - Regex: StdAfx.h + Priority: -1 + - Regex: ^<.*\.h> + Priority: 1 + - Regex: ^<.* + Priority: 2 + - Regex: .* + Priority: 3 +IncludeIsMainRegex: ([-_](test|unittest))?$ +# Indent case labels inside switch statements +IndentCaseLabels: true +# Indent code blocks inside extern blocks +IndentExternBlock: Indent +# Do not indent preprocessor directives +IndentPPDirectives: None +# Use 4 spaces for indentation +IndentWidth: 4 +# Do not indent wrapped function names after the return type +IndentWrappedFunctionNames: false +# Do not keep empty lines at the start of code blocks +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +# Do not allow consecutive empty lines +MaxEmptyLinesToKeep: 1 +# Do not indent contents inside namespaces +NamespaceIndentation: None +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +# * and & should be placed next to the type name +PointerAlignment: Left +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - c++ + - C++ + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + CanonicalDelimiter: '' + BasedOnStyle: google +# Allow reflowing of comments +ReflowComments: true +# Allow sorting of #include statements +SortIncludes: true +# Allow sorting of using declarations +SortUsingDeclarations: true +# Do not include spaces after C-style casts +SpaceAfterCStyleCast: false +# Require a space after the template keyword +SpaceAfterTemplateKeyword: true +# Require spaces around assignment operators +SpaceBeforeAssignmentOperators: true +# Do not include spaces before C++11 braced initializer lists +SpaceBeforeCpp11BracedList: false +# Require a space before the colon in constructor initializers +SpaceBeforeCtorInitializerColon: true +# Require a space before the colon in inheritance statements +SpaceBeforeInheritanceColon: true +# Require a space after control statement keywords (if, for, while, etc.) +SpaceBeforeParens: ControlStatements +# Require a space before the colon in range-based for loops +SpaceBeforeRangeBasedForLoopColon: true +# Do not include spaces inside empty parentheses +SpaceInEmptyParentheses: false +# Require a space before // in trailing comments +SpacesBeforeTrailingComments: 1 +# Do not include spaces around angle brackets +SpacesInAngles: false +# Do not include spaces around C-style cast parentheses +SpacesInCStyleCastParentheses: false +# Do not include spaces around parentheses +SpacesInParentheses: false +# Do not include spaces around square brackets +SpacesInSquareBrackets: false +Standard: Auto +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..820d51d --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,78 @@ +Checks: > + *, + -abseil-*, + -altera-*, + -android-*, + -boost-*, + -cert-*, + -cppcoreguidelines-*, + -darwin-*, + -fuchsia-*, + -google-*, + -hicpp-*, + -linuxkernel-*, + -llvm-*, + -llvmlibc-*, + -mpi-*, + -objc-*, + -openmp-*, + -zircon-*, + cert-err34-c, + google-explicit-constructor, + cppcoreguidelines-rvalue-reference-param-not-moved, + -bugprone-assignment-in-if-condition, + -bugprone-branch-clone, + -bugprone-easily-swappable-parameters, + -bugprone-empty-catch, + -bugprone-macro-parentheses, + -bugprone-narrowing-conversions, + -bugprone-signed-char-misuse, + -bugprone-switch-missing-default-case, + -bugprone-unchecked-optional-access, + -clang-analyzer-*, + -concurrency-mt-unsafe, + -misc-const-correctness, + -misc-no-recursion, + -misc-non-private-member-variables-in-classes, + -misc-throw-by-value-catch-by-reference, + -misc-use-anonymous-namespace, + -misc-include-cleaner, + -misc-unused-using-decls, + -modernize-avoid-c-arrays, + -modernize-deprecated-ios-base-aliases, + -modernize-loop-convert, + -modernize-macro-to-enum, + -modernize-raw-string-literal, + -modernize-replace-auto-ptr, + -modernize-return-braced-init-list, + -modernize-type-traits, + -modernize-use-auto, + -modernize-use-nodiscard, + -modernize-use-trailing-return-type, + -modernize-concat-nested-namespaces, + -performance-avoid-endl, + -performance-enum-size, + -performance-inefficient-string-concatenation, + -performance-no-automatic-move, + -performance-noexcept-swap, + -portability-simd-intrinsics, + -portability-std-allocator-const, + -readability-avoid-const-params-in-decls, + -readability-avoid-nested-conditional-operator, + -readability-braces-around-statements, + -readability-container-data-pointer, + -readability-function-cognitive-complexity, + -readability-function-size, + -readability-identifier-length, + -readability-identifier-naming, + -readability-implicit-bool-conversion, + -readability-isolate-declaration, + -readability-magic-numbers, + -readability-suspicious-call-argument, + -readability-uppercase-literal-suffix +HeaderFilterRegex: (src)\/[a-z]+\.hpp +CheckOptions: + - key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic + value: '1' + - key: readability-simplify-boolean-expr.SimplifyDeMorgan + value: '0' diff --git a/.copier-answers.yml b/.copier-answers.yml new file mode 100644 index 0000000..ab99fda --- /dev/null +++ b/.copier-answers.yml @@ -0,0 +1,27 @@ +# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY +_commit: v0.8.25 +_src_path: https://github.com/serious-scaffold/ss-pybind11 +author_email: msclock@126.com +author_name: l.feng +codecov_notify_builds: 1 +codecov_threshold: 5% +compiled: true +copyright_holder: l.feng +copyright_license: Apache Software License +copyright_year: '2024' +create_devcontainer: true +default_py: '3.10' +docs_type: sphinx +max_py: '3.13' +min_py: '3.8' +module_name: pybit7z +organization_name: msclock +project_description: A wrapper based on bit7z. +project_name: pybit7z +repo_name: pybit7z +repo_namespace: msclock +repo_platform: github +use_codecov: true +use_codeql: true +use_conan: false +use_cpm: false diff --git a/.cppcheck-suppressions.txt b/.cppcheck-suppressions.txt new file mode 100644 index 0000000..3384b4a --- /dev/null +++ b/.cppcheck-suppressions.txt @@ -0,0 +1,2 @@ +// Limit analysis of branches. Use --check-level=exhaustive to analyze all branches. +normalCheckLevelMaxBranches diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..149f413 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,38 @@ +// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: +// https://github.com/devcontainers/images/tree/main/src/base-ubuntu +{ + "name": "pybit7z", + "image": "mcr.microsoft.com/devcontainers/base:ubuntu-24.04", + "capAdd": [ + // Enable ptrace-based debugging for C++ + "SYS_PTRACE" + ], + "securityOpt": [ + "seccomp=unconfined" + ], + // https://github.com/devcontainers/features + // Using features in vscode requires access github with uncomment the below + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "username": "vscode" + }, + "ghcr.io/devcontainers/features/docker-in-docker:2": {}, + "ghcr.io/devcontainers/features/python:1": {}, + "ghcr.io/msclock/features/vcpkg:2": {} + }, + "customizations": { + "vscode": { + "extensions": [ + "EditorConfig.EditorConfig", + "mhutchie.git-graph", + "ms-vscode.cmake-tools" + ] + } + }, + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": { + "git-completions": "echo 'source /usr/share/bash-completion/completions/git' >> ~/.bashrc" + }, + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + "remoteUser": "vscode" +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4f6c70c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +# EditorConfig is awesome: https://EditorConfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{yml,yaml,json,toml,jinja,css,js,cmake}] +indent_size = 2 + +[CMakeLists.txt] +indent_size = 2 + +[Makefile] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..a7fe8ee --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +# Keep compatilibity between windows and unix-like OS +* text=auto eol=lf +*.{cmd,[cC][mM][dD]} text eol=crlf +*.{bat,[bB][aA][tT]} text eol=crlf + +# Archive Format +*.tar.gz filter=lfs diff=lfs merge=lfs -text diff --git a/.github/codecov.yml b/.github/codecov.yml new file mode 100644 index 0000000..83fd753 --- /dev/null +++ b/.github/codecov.yml @@ -0,0 +1,12 @@ +codecov: + notify: + after_n_builds: 1 +coverage: + status: + project: + default: + target: auto + threshold: 5% + patch: + default: + informational: true diff --git a/.github/codeql-config.yml b/.github/codeql-config.yml new file mode 100644 index 0000000..8de2daf --- /dev/null +++ b/.github/codeql-config.yml @@ -0,0 +1,6 @@ +name: CodeQL config + +# https://codeql.github.com/codeql-query-help/ +query-filters: + - exclude: + id: py/commented-out-code diff --git a/.github/matchers/pylint.json b/.github/matchers/pylint.json new file mode 100644 index 0000000..e3a6bd1 --- /dev/null +++ b/.github/matchers/pylint.json @@ -0,0 +1,32 @@ +{ + "problemMatcher": [ + { + "severity": "warning", + "pattern": [ + { + "regexp": "^([^:]+):(\\d+):(\\d+): ([A-DF-Z]\\d+): \\033\\[[\\d;]+m([^\\033]+).*$", + "file": 1, + "line": 2, + "column": 3, + "code": 4, + "message": 5 + } + ], + "owner": "pylint-warning" + }, + { + "severity": "error", + "pattern": [ + { + "regexp": "^([^:]+):(\\d+):(\\d+): (E\\d+): \\033\\[[\\d;]+m([^\\033]+).*$", + "file": 1, + "line": 2, + "column": 3, + "code": 4, + "message": 5 + } + ], + "owner": "pylint-error" + } + ] +} diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..c88a569 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,79 @@ +name: CD + +on: + workflow_dispatch: + schedule: + - cron: 0 0 * * 5 + release: + types: + - published + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + FORCE_COLOR: 3 + +jobs: + build_sdist: + name: Build SDist + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + + - name: Build SDist + run: pipx run build --sdist + + - uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4 + with: + name: cibw-sdist + path: dist/*.tar.gz + + + build_wheels: + uses: ./.github/workflows/reuseable_cibuildwheel.yml + secrets: inherit + with: + auto: true + extra: true + + + check_dist: + name: Check dist + needs: [build_wheels, build_sdist] + runs-on: ubuntu-24.04 + steps: + - uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4 + with: + pattern: cibw-* + path: dist + merge-multiple: true + + - run: pipx run twine check --strict dist/* + + + upload_all: + needs: [build_wheels, build_sdist, check_dist] + environment: + name: pypi + url: https://pypi.org/project/pybit7z + permissions: + id-token: write + runs-on: ubuntu-latest + if: github.event_name == 'release' && github.event.action == 'published' + + steps: + - uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4 + with: + pattern: cibw-* + path: dist + merge-multiple: true + + - uses: pypa/gh-action-pypi-publish@release/v1 + env: + PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }} + with: + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..cdf2c73 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,137 @@ +name: CI + +on: + workflow_dispatch: + + push: + branches: + - master + - beta + - alpha + - '*.x' + + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + FORCE_COLOR: 3 + +jobs: + pre-commit: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + + - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5 + with: + python-version: 3.x + + - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + with: + extra_args: --hook-stage manual --all-files + + - name: Run PyLint + run: | + echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json" + pipx run nox -s pylint + + + wheels: + needs: [pre-commit] + uses: ./.github/workflows/reuseable_cibuildwheel.yml + secrets: inherit + with: + auto: true + extra: false + + + codecov: + runs-on: ubuntu-24.04 + needs: [pre-commit] + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + + - name: Cache + uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4 + with: + path: | + ~/vcpkg + ~/.cache/pip + ~/.cache/vcpkg + key: codecov-${{ hashFiles('vcpkg.json') }} + restore-keys: codecov-${{ hashFiles('vcpkg.json') }} + + - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5 + with: + python-version: 3.x + + - name: Install package + run: python -m pip install .[test] -v + + - name: Test package + run: >- + python -m pytest -ra --cov --cov-report=xml --cov-report=term + --durations=20 + + - name: Upload coverage report + uses: codecov/codecov-action@7f8b4b4bde536c465e797be725718b88c5d95e0e # v5.1.1 + + + check-docs: + runs-on: ubuntu-24.04 + needs: [pre-commit] + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + + - name: Cache + uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4 + with: + path: | + ~/vcpkg + ~/.cache/pip + ~/.cache/vcpkg + key: docs-${{ hashFiles('vcpkg.json') }} + restore-keys: docs-${{ hashFiles('vcpkg.json') }} + + - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5 + with: + python-version: 3.x + + - name: Checks the docs with warnings as errors + run: pipx run nox -s docs -- --check + + - name: Checks the docs for broken links + run: pipx run nox -s docs -- --linkcheck + + + pass: + if: always() + needs: + - wheels + - codecov + - check-docs + runs-on: ubuntu-24.04 + timeout-minutes: 2 + permissions: + pull-requests: write + + steps: + - name: Decide whether the needed jobs succeeded or failed + uses: re-actors/alls-green@release/v1 + with: + jobs: ${{ toJSON(needs) }} + + - name: Approve pr if all jobs succeeded + if: contains(github.event.pull_request.labels.*.name, 'auto-approval') && contains(github.actor, '[bot]') + uses: hmarr/auto-approve-action@v4 diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..a834d63 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,74 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: CodeQL + +on: + push: + branches: [master] + pull_request: + # The branches below must be a subset of the branches above + branches: [master] + schedule: + - cron: 15 4 * * 3 + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-24.04 + permissions: + actions: read + contents: read + security-events: write + strategy: + fail-fast: false + matrix: + language: [python, cpp] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://git.io/codeql-language-support + + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + + - name: Initialize CodeQL + uses: github/codeql-action/init@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6 + with: + languages: ${{ matrix.language }} + config-file: ./.github/codeql-config.yml + + - name: Cache + uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4 + with: + path: | + ~/vcpkg + ~/.cache/vcpkg + key: codeql-${{ hashFiles('vcpkg.json') }} + restore-keys: codeql-${{ hashFiles('vcpkg.json') }} + + - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5 + with: + python-version: 3.x + + - name: Build + run: python -m pip install .[test] + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6 diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml new file mode 100644 index 0000000..34a2d52 --- /dev/null +++ b/.github/workflows/pages.yml @@ -0,0 +1,59 @@ +name: Pages + +on: + workflow_dispatch: + + push: + branches: + - master + - beta + - alpha + - '*.x' + + release: + types: + - published + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + FORCE_COLOR: 3 + +jobs: + pages: + name: Deploy to GitHub Pages + runs-on: ubuntu-24.04 + permissions: + contents: write + id-token: write + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + ref: ${{ github.head_ref }} + + - name: Cache + uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4 + with: + path: | + ~/vcpkg + ~/.cache/pip + ~/.cache/vcpkg + key: cd-pages-${{ hashFiles('vcpkg.json') }} + restore-keys: cd-pages-${{ hashFiles('vcpkg.json') }} + + - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5 + with: + python-version: 3.x + + - name: Build Docs + run: pipx run nox -s docs + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./docs/_build/html diff --git a/.github/workflows/renovate.yml b/.github/workflows/renovate.yml new file mode 100644 index 0000000..321141e --- /dev/null +++ b/.github/workflows/renovate.yml @@ -0,0 +1,52 @@ +name: Renovate + +on: + schedule: + # Match renovate schedule:earlyMondays and schedule:automergeMonthly + - cron: '*/15 0-3 * * *' + workflow_dispatch: + +jobs: + renovate: + container: + env: + LOG_LEVEL: debug + RENOVATE_ALLOWED_POST_UPGRADE_COMMANDS: '["^git", "^find"]' + RENOVATE_BRANCH_PREFIX: renovate-github/ + RENOVATE_ENABLED: ${{ vars.RENOVATE_ENABLED || true }} + RENOVATE_ENABLED_MANAGERS: '["pep621", "copier", "github-actions", "regex", "pre-commit"]' + RENOVATE_OPTIMIZE_FOR_DISABLED: 'true' + RENOVATE_PLATFORM: github + RENOVATE_REPOSITORIES: '["${{ github.repository }}"]' + RENOVATE_REPOSITORY_CACHE: enabled + image: ghcr.io/renovatebot/renovate:39.49.0@sha256:9f44b9392d1da54a8f59dfd73d424d7d151787cd18d6f14778bcb8451ee07543 + options: --user root + runs-on: ubuntu-24.04 + steps: + - run: env | sort + + - name: Generate a token with GitHub App if App ID exists + id: generate-token + if: vars.BOT_APP_ID + uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1 + with: + app-id: ${{ vars.BOT_APP_ID }} + private-key: ${{ secrets.BOT_PRIVATE_KEY }} + + - name: Warn if RENOVATE_GIT_AUTHOR is set while using GitHub App token + if: steps.generate-token.outputs.token && vars.RENOVATE_GIT_AUTHOR + run: | + echo "# :warning: `RENOVATE_GIT_AUTHOR` is set explicitly while using GitHub App token" >> $GITHUB_STEP_SUMMARY + echo "Generally, Renovate automatically detects the git author and email using the token. However, explicitly setting the `RENOVATE_GIT_AUTHOR` will override this behavior." >> $GITHUB_STEP_SUMMARY + + - name: Run Renovate + env: + RENOVATE_PLATFORM_COMMIT: ${{ steps.generate-token.outputs.token && true || false }} + RENOVATE_TOKEN: ${{ secrets.RENOVATE_TOKEN || steps.generate-token.outputs.token }} + RENOVATE_GIT_AUTHOR: ${{ vars.RENOVATE_GIT_AUTHOR }} + run: | + if [ -z "$RENOVATE_TOKEN" ]; then + echo "RENOVATE_TOKEN is not properly configured, skipping ..." + else + renovate $RENOVATE_EXTRA_FLAG + fi diff --git a/.github/workflows/reuseable_cibuildwheel.yml b/.github/workflows/reuseable_cibuildwheel.yml new file mode 100644 index 0000000..f327399 --- /dev/null +++ b/.github/workflows/reuseable_cibuildwheel.yml @@ -0,0 +1,67 @@ +name: cibuildwheel + +on: + workflow_call: + inputs: + auto: + description: Build wheels for auto archs + required: true + type: boolean + extra: + description: Build wheels for aarch64 armv7l ppc64le & s390x + required: true + type: boolean + +jobs: + build_wheels: + name: On ${{ matrix.os }}, with ${{ matrix.arch }}, build ${{ matrix.build }} wheels + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ubuntu-24.04] # renovate: github-runner + arch: [aarch64, ppc64le, s390x, armv7l, x86_64, i686] + build: [manylinux, musllinux] + qemu_arch: [aarch64 ppc64le s390x armv7l] + include: + - {os: windows-2019, arch: AMD64, build: win} + - {os: windows-2019, arch: x86, build: win} + - {os: windows-2022, arch: ARM64, build: win} + - {os: macos-13, arch: x86_64, build: macos} + - {os: macos-14, arch: arm64, build: macos} + + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + if: > + inputs.auto && !contains(matrix.qemu_arch, matrix.arch) || + inputs.extra && contains(matrix.qemu_arch, matrix.arch) + with: + fetch-depth: 0 + + - name: Set up msvc on Windows + if: runner.os == 'Windows' && inputs.auto && !contains(matrix.qemu_arch, matrix.arch) + uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1 + with: + arch: ${{ matrix.arch }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0 + if: runner.os == 'Linux' && inputs.extra && contains(matrix.qemu_arch, matrix.arch) + + - name: Build wheels + if: > + inputs.auto && !contains(matrix.qemu_arch, matrix.arch) || + inputs.extra && contains(matrix.qemu_arch, matrix.arch) + uses: pypa/cibuildwheel@ee63bf16da6cddfb925f542f2c7b59ad50e93969 # v2.22.0 + env: + CIBW_ARCHS: ${{ matrix.arch }} + CIBW_BUILD: '*${{ matrix.build }}*' + + - name: Upload wheels + if: > + inputs.auto && !contains(matrix.qemu_arch, matrix.arch) || + inputs.extra && contains(matrix.qemu_arch, matrix.arch) + uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4 + with: + name: cibw-${{ matrix.os }}-${{ matrix.build }}-${{ matrix.arch }} + path: wheelhouse/*.whl diff --git a/.github/workflows/semantic-release.yml b/.github/workflows/semantic-release.yml new file mode 100644 index 0000000..63c4d3c --- /dev/null +++ b/.github/workflows/semantic-release.yml @@ -0,0 +1,60 @@ +name: Semantic Release + +on: + workflow_run: + workflows: [CI] + types: [completed] + branches: + - master + - beta + - alpha + - '*.x' + +jobs: + semantic-release: + name: Semantic Release + runs-on: ubuntu-24.04 + if: github.event.workflow_run.conclusion == 'success' && github.repository == 'msclock/pybit7z' + permissions: + contents: write + id-token: write + issues: write + pull-requests: write + steps: + - name: Generate a bot token with BOT_APP_ID + id: bot_token + if: vars.BOT_APP_ID && env.BOT_PRIVATE_KEY != null + uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1 + env: + BOT_PRIVATE_KEY: ${{ secrets.BOT_PRIVATE_KEY }} + with: + app-id: ${{ vars.BOT_APP_ID }} + private-key: ${{ secrets.BOT_PRIVATE_KEY }} + + - name: Warn if use GITHUB_TOKEN + if: env._GITHUB_TOKEN == env._TO_BE_VERIFIED_TOKEN + env: + _GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + _TO_BE_VERIFIED_TOKEN: ${{ steps.bot_token.outputs.token || secrets.PAT || secrets.GITHUB_TOKEN }} + run: | + echo "# :warning: GITHUB_TOKEN is used" >> $GITHUB_STEP_SUMMARY + echo "The GITHUB_TOKEN is used instead of a bot token or PAT that will not emit the released publish event for triggering a released workflow." >> $GITHUB_STEP_SUMMARY + + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + with: + fetch-depth: 0 + token: ${{ steps.bot_token.outputs.token || secrets.PAT || secrets.GITHUB_TOKEN }} + + - name: Setup Node.js + uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4 + with: + node-version: lts/* + + - name: Semantic Release + env: + GITHUB_TOKEN: ${{ steps.bot_token.outputs.token || secrets.PAT || secrets.GITHUB_TOKEN }} + run: > + npx + --package conventional-changelog-conventionalcommits@7 + --package semantic-release@22 + semantic-release diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03c01d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,366 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +build +.build + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# OS specific stuff +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Common editor files +*~ +*.swp + +# Build output +*.out +/out +dist + +# Python lint or env cache +.nox +.mypy_cache + +# Documenation files +docs/_build + +# Vcpkg integration files +!cmake/vcpkg/**/* + +# setuptools_scm +src/*/_version.py + +# ruff +.ruff_cache/ diff --git a/.markdownlint.yaml b/.markdownlint.yaml new file mode 100644 index 0000000..a435c58 --- /dev/null +++ b/.markdownlint.yaml @@ -0,0 +1,10 @@ +default: true + +# Multiple headings with the same content +MD024: false +# First line in a file should be a top-level heading +MD041: false +line_length: false +no-hard-tabs: false +whitespace: false +html: false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..ce084e7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,158 @@ +# To use: +# +# pre-commit run -a +# +# Or: +# +# pre-commit install # (runs every time you commit in git) +# +# To update this file: +# +# pre-commit autoupdate +# +# See https://github.com/pre-commit/pre-commit + +fail_fast: false +exclude: | + (?x)( + ^(.vscode/.*) + ) +files: '' # set '' as default + +repos: + # Standard hooks + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-added-large-files + args: [--maxkb=4096] + - id: check-case-conflict + - id: check-merge-conflict + - id: check-symlinks + - id: debug-statements + - id: end-of-file-fixer + - id: mixed-line-ending + - id: name-tests-test + args: [--pytest-test-first] + - id: requirements-txt-fixer + - id: trailing-whitespace + + # Check RST + - repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.10.0 + hooks: + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal + + # Strong lint from ruff + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.2 + hooks: + - id: ruff + args: [--fix, --show-fixes] + - id: ruff-format + + # Mypy Check + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.13.0 + hooks: + - id: mypy + files: src|tests + args: [] + additional_dependencies: + - pytest + - importlib_metadata + + # Check yaml + - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks + rev: v2.14.0 + hooks: + - id: pretty-format-yaml + args: + - --autofix + - --offset=2 + exclude: | + (?x)( + .copier-answers.yml + ) + + # Changes tabs to spaces + - repo: https://github.com/Lucas-C/pre-commit-hooks + rev: v1.5.5 + hooks: + - id: remove-tabs + + # CMake formatting + - repo: https://github.com/cheshirekow/cmake-format-precommit + rev: v0.6.13 + hooks: + - id: cmake-format + additional_dependencies: [pyyaml] + types: [file] + files: (\.cmake|CMakeLists.txt)(.in)?$ + exclude: | + (?x)( + ^(cmake/vcpkg/ports/.*) | + ^(cmake/vcpkg/triplets/.*) | + ^(cmake/vcpkg/scripts/toolchains/.*) + ) + + # Check pyproject + - repo: https://github.com/abravalheri/validate-pyproject + rev: v0.23 + hooks: + - id: validate-pyproject + additional_dependencies: ['validate-pyproject-schema-store[all]>=2024.10.21'] + + # Check jsonschema + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.30.0 + hooks: + - id: check-dependabot + - id: check-github-workflows + - id: check-readthedocs + + # Check for spelling + - repo: https://github.com/codespell-project/codespell + rev: v2.3.0 + hooks: + - id: codespell + args: + - -L=lang + - --check-filenames + - --write-changes + + # Check for common shell mistakes + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.9.0.6 + hooks: + - id: shellcheck + + # Clang format the codebase automatically + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.4 + hooks: + - id: clang-format + types_or: [c++, c, cuda] + + # Check for markdown + - repo: https://github.com/igorshubovych/markdownlint-cli + rev: v0.43.0 + hooks: + - id: markdownlint-fix + + # Check for renovate config + - repo: https://github.com/renovatebot/pre-commit-hooks + rev: 39.49.0 + hooks: + - id: renovate-config-validator + stages: [manual] + + - repo: local + hooks: + - id: disallow-caps + name: Disallow improper capitalization + language: pygrep + entry: PyBind|Numpy|Cmake|CCache|Github|PyTest + exclude: .pre-commit-config.yaml diff --git a/.releaserc.js b/.releaserc.js new file mode 100644 index 0000000..f7a2872 --- /dev/null +++ b/.releaserc.js @@ -0,0 +1,67 @@ +module.exports = { + branches: [ + '+([0-9])?(.{+([0-9]),x}).x', + { + name: 'beta', + prerelease: true + }, + { + name: 'alpha', + prerelease: true + }, + 'master', + ], + dryRun: false, + plugins: [ + [ + "@semantic-release/commit-analyzer", + { + preset: "angular", + releaseRules: [ + { breaking: true, release: "major" }, + { type: "feat", release: "minor" }, + { type: "fix", release: "patch" }, + { type: "style", release: false }, + { type: "refactor", release: false }, + { type: "perf", release: "patch" }, + { type: "revert", release: "patch" }, + { type: "chore", release: false }, + { type: "build", release: false }, + { type: "ci", release: false }, + { type: "docs", release: false }, + { type: "test", release: false }, + { scope: '*no-release*', release: false }, + ], + parseOpts: { + noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"] + }, + }, + ], + [ + "@semantic-release/release-notes-generator", + { + preset: "conventionalcommits", + presetConfig: { + types: [ + { type: "feat", section: "Features" }, + { type: "fix", section: "Bug Fixes" }, + { type: "style", section: "Style" }, + { type: "refactor", section: "Refactor" }, + { type: "perf", section: "Performance" }, + { type: "revert", section: "Reverts" }, + { type: "chore", section: "Chores" }, + { type: "build", section: "Build" }, + { type: "ci", section: "CI" }, + { type: "docs", section: "Docs" }, + { type: "test", section: "Tests" }, + ], + }, + parserOpts: { + headerPattern: "^(\\w*)(?:\\((?:no-release,?)?([\\w\\$\\.\\-\\* ]*)(?:,?no-release)?\\))?\\: (.*)$", + noteKeywords: ["BREAKING CHANGE", "BREAKING CHANGES", "BREAKING"] + }, + }, + ], + "@semantic-release/github", + ], +}; diff --git a/.renovaterc.json b/.renovaterc.json new file mode 100644 index 0000000..e472df5 --- /dev/null +++ b/.renovaterc.json @@ -0,0 +1,101 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:recommended", + "helpers:pinGitHubActionDigests", + ":enablePreCommit" + ], + "automergeType": "pr", + "automergeStrategy": "merge-commit", + "ignoreScripts": false, + "platformAutomerge": false, + "packageRules": [ + { + "automerge": true, + "addLabels": [ + "auto-approval" + ], + "extends": [ + "schedule:earlyMondays", + "schedule:automergeMonthly" + ], + "matchUpdateTypes": [ + "minor", + "patch", + "pin", + "digest" + ] + }, + { + "description": "Group renovate docker tag and pre-commit-hooks tag", + "groupName": "renovate group", + "addLabels": [ + "renovate" + ], + "matchDatasources": [ + "docker", + "github-tags" + ], + "matchDepNames": [ + "ghcr.io/renovatebot/renovate", + "renovatebot/pre-commit-hooks" + ] + } + ], + "customManagers": [ + { + "description": "Update python packages installed with pip or pipx", + "customType": "regex", + "datasourceTemplate": "pypi", + "fileMatch": [ + "^\\.github/workflows/.+\\.yml$", + "^\\.renovaterc\\.json$" + ], + "matchStrings": [ + "pipx? install.* (?.*?)==(?.*?)[\\s\";]" + ] + }, + { + "customType": "regex", + "datasourceTemplate": "github-runners", + "depTypeTemplate": "github-runner", + "description": "Match GitHub runner defined in GitHub Actions matrix strategy", + "fileMatch": [ + "^\\.github/workflows/.+\\.yml$" + ], + "matchStringsStrategy": "recursive", + "matchStrings": [ + "[-:] (.*) # renovate: github-runner", + "(?ubuntu|windows|macos)-(?[\\d\\.\\-\\w]+)" + ], + "versioningTemplate": "docker" + }, + { + "description": "Update microsoft vcpkg baseline", + "customType": "regex", + "currentValueTemplate": "master", + "datasourceTemplate": "git-refs", + "depNameTemplate": "https://github.com/microsoft/vcpkg", + "depTypeTemplate": "vcpkg-baseline", + "fileMatch": [ + "^vcpkg\\.json$" + ], + "matchStrings": [ + "\"builtin-baseline\": \"(?.*?)\"" + ] + }, + { + "description": "Update custom registry baseline", + "customType": "regex", + "currentValueTemplate": "master", + "datasourceTemplate": "git-refs", + "depTypeTemplate": "vcpkg-baseline", + "fileMatch": [ + "^vcpkg\\.json$" + ], + "matchStrings": [ + "\"baseline\": \"(?.*?)\"[^}]+?\"repository\": \"(?.*?)\"" + ] + } + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4abfecb --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,32 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${command:cmake.launchTargetPath}", + "args": [], + "stopAtEntry": false, + "cwd": "${command:cmake.launchTargetDirectory}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f8b1473 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,108 @@ +{ + "python.testing.pytestArgs": [ + "tests", + "-ra", + "--showlocals", + "--strict-markers", + "--strict-config" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, + "cmake.configureOnEdit": false, + "cmake.options.statusBarVisibility": "icon", + "cmake.useCMakePresets": "always", + "files.associations": { + "iosfwd": "cpp", + "type_traits": "cpp", + "algorithm": "cpp", + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "filesystem": "cpp", + "format": "cpp", + "forward_list": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "queue": "cpp", + "random": "cpp", + "ranges": "cpp", + "ratio": "cpp", + "set": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "utility": "cpp", + "variant": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp", + "string_view": "cpp", + "*.tcc": "cpp", + "cwctype": "cpp", + "memory_resource": "cpp", + "numbers": "cpp", + "semaphore": "cpp", + "codecvt": "cpp", + "source_location": "cpp" + } +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..96abdf6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.25) +message(STATUS "CMAKE VERSION:${CMAKE_VERSION}") + +if(NOT DEFINED SKBUILD_PROJECT_NAME) + set(SKBUILD_PROJECT_NAME _core) + set(SKBUILD_PROJECT_VERSION 0.0.0) +endif() + +message( + STATUS + "Building CMake project ${SKBUILD_PROJECT_NAME} ${SKBUILD_PROJECT_VERSION}") + +project( + ${SKBUILD_PROJECT_NAME} + VERSION ${SKBUILD_PROJECT_VERSION} + LANGUAGES C CXX) + +set(CMAKE_CXX_STANDARD + 20 + CACHE STRING "C++ standard") +set(CMAKE_CXX_STANDARD_REQUIRED + ON + CACHE BOOL "C++ standard required") +set(CMAKE_CXX_EXTENSIONS + OFF + CACHE BOOL "C++ extensions") + +# Project default module +find_package(cmake-modules REQUIRED) +include(cmake-modules/ProjectDefault) + +# Project custom modules +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +include(ConfigureWarningsAndHardening) +include(ConfigureVersion) + +add_subdirectory(src) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..19edd87 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,19 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 25, + "patch": 0 + }, + "include": [ + "cmake/presets/default.json", + "cmake/presets/x64-linux-llvm.json", + "cmake/presets/x64-linux-gcc.json", + "cmake/presets/x64-mingw-dynamic-windows-mingw.json", + "cmake/presets/x64-mingw-static-windows-mingw.json", + "cmake/presets/x64-osx-gcc.json", + "cmake/presets/x64-osx-llvm.json", + "cmake/presets/x64-windows-llvm.json", + "cmake/presets/x64-windows-msvc.json" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf34ea0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2024 l.feng + + 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a65177b --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# pybit7z + + + +A wrapper based on bit7z. + +[![Documentation](https://img.shields.io/badge/Documentation-sphinx-blue)](https://msclock.github.io/pybit7z) +[![License](https://img.shields.io/github/license/msclock/pybit7z)](https://github.com/msclock/pybit7z/blob/master/LICENSE) +[![SS Badge](https://img.shields.io/badge/Serious%20Scaffold-pybind11-blue)](https://github.com/serious-scaffold/ss-pybind11) + +[![CI](https://github.com/msclock/pybit7z/actions/workflows/ci.yml/badge.svg)](https://github.com/msclock/pybit7z/actions/workflows/ci.yml) +[![CD](https://github.com/msclock/pybit7z/actions/workflows/cd.yml/badge.svg)](https://github.com/msclock/pybit7z/actions/workflows/cd.yml) +[![Renovate](https://github.com/msclock/pybit7z/actions/workflows/renovate.yml/badge.svg)](https://github.com/msclock/pybit7z/actions/workflows/renovate.yml) +[![Semantic Release](https://github.com/msclock/pybit7z/actions/workflows/semantic-release.yml/badge.svg)](https://github.com/msclock/pybit7z/actions/workflows/semantic-release.yml) +[![codecov](https://codecov.io/gh/msclock/pybit7z/branch/master/graph/badge.svg?token=123456789)](https://codecov.io/gh/msclock/pybit7z) + +[![Release](https://img.shields.io/github/v/release/msclock/pybit7z)](https://github.com/msclock/pybit7z/releases) +[![PyPI](https://img.shields.io/pypi/v/pybit7z)](https://pypi.org/project/pybit7z/) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pybit7z)](https://pypi.org/project/pybit7z/) + +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) +[![clang-format](https://img.shields.io/badge/clang--format-enabled-blue)](https://github.com/pre-commit/mirrors-clang-format) +[![cmake-format](https://img.shields.io/badge/cmake--format-enabled-blue)](https://github.com/cheshirekow/cmake-format-precommit) +[![codespell](https://img.shields.io/badge/codespell-enabled-blue)](https://github.com/codespell-project/codespell) +[![markdownlint](https://img.shields.io/badge/markdownlint-enabled-blue)](https://github.com/igorshubovych/markdownlint-cli) +[![shellcheck](https://img.shields.io/badge/shellcheck-enabled-blue)](https://github.com/shellcheck-py/shellcheck-py) + + + +## License + +Apache Software License, for more details, see the [LICENSE](https://github.com/msclock/pybit7z/blob/master/LICENSE) file. diff --git a/cmake/ConfigureVersion.cmake b/cmake/ConfigureVersion.cmake new file mode 100644 index 0000000..569c2ea --- /dev/null +++ b/cmake/ConfigureVersion.cmake @@ -0,0 +1,48 @@ +#[[ +This file detects the git commit information and sets the version number accordingly. + +Detected variables: + - CMAKE_PROJECT_GIT_COMMIT_*: git commit information (see below) + - CMAKE_PROJECT_VERSION: version number (major.minor.patch.tweak) + - CMAKE_PROJECT_VERSION_: version numbers (as integers) + +]] + +include_guard(GLOBAL) +include(cmake-modules/configure/GitTools) + +find_package(robotology-cmake-ycm REQUIRED) +include(robotology-cmake-ycm/modules/GitInfo) +include(robotology-cmake-ycm/modules/ExtractVersion) + +# Retrieve the git commit information to CMAKE_PROJECT_GIT_WT_* variables +git_wt_info(SOURCE_DIR ${CMAKE_SOURCE_DIR} PREFIX CMAKE_PROJECT) + +# Rename CMAKE_PROJECT_GIT_WT_* variables to CMAKE_PROJECT_GIT_COMMIT_* +# variables +get_cmake_property(_vars VARIABLES) +foreach(_var IN LISTS _vars) + if(_var MATCHES "^CMAKE_PROJECT_GIT_WT_") + string(REGEX REPLACE "^CMAKE_PROJECT_GIT_WT_" "CMAKE_PROJECT_GIT_COMMIT_" + new_name "${_var}") + set(${new_name} "${${_var}}") + unset(new_name) + endif() +endforeach() + +# Set the CMAKE_PROJECT_VERSION variable +if(NOT CMAKE_PROJECT_GIT_COMMIT_TAG) + set(CMAKE_PROJECT_VERSION "0.0.0.0") +else() + set(CMAKE_PROJECT_VERSION "${CMAKE_PROJECT_GIT_COMMIT_TAG}") + + if(CMAKE_PROJECT_GIT_COMMIT_TAG_REVISION) + set(CMAKE_PROJECT_VERSION + "${CMAKE_PROJECT_VERSION}.${CMAKE_PROJECT_GIT_COMMIT_TAG_REVISION}") + endif() + + string(REGEX REPLACE "^v" "" CMAKE_PROJECT_VERSION "${CMAKE_PROJECT_VERSION}") +endif() + +# Set the CMAKE_PROJECT_VERSION_ variables +extract_version(CMAKE_PROJECT CMAKE_PROJECT REVERSE_NAME) diff --git a/cmake/ConfigureWarningsAndHardening.cmake b/cmake/ConfigureWarningsAndHardening.cmake new file mode 100644 index 0000000..784fd89 --- /dev/null +++ b/cmake/ConfigureWarningsAndHardening.cmake @@ -0,0 +1,117 @@ +#[[ +This file configures the following things: + - dynamic tools: sanitizers, valgrind. + - static tools: clang-tidy, cppcheck. + - compiler flags + - hardening options + +]] + +include_guard(GLOBAL) + +# ############################################################################## +# Sanitizer +# ############################################################################## + +set(USE_SANITIZER + OFF + CACHE BOOL "Enable sanitizer") + +include(cmake-modules/build/Sanitizer) + +# ############################################################################## +# Valgrind +# ############################################################################## + +set(USE_VALGRIND + OFF + CACHE BOOL "Enable Valgrind") +set(USE_VALGRIND_OPTIONS + --leak-check=full # Each individual leak will be shown in detail. + --show-leak-kinds=all # Show all of "definite, indirect, possible, + # reachable" leak kinds in the "full" report. + --gen-suppressions=all # gen suppress info automatically. + --track-origins=yes # Favor useful output over speed. This tracks the + # origins of uninitialized values, which could be very + # useful for memory errors. Consider turning off if + # Valgrind is unacceptably slow. + CACHE STRING "valgrind options.") + +include(cmake-modules/test/Valgrind) + +# ############################################################################## +# Clang-Tidy +# ############################################################################## + +set(USE_CLANGTIDY + OFF + CACHE BOOL "Enable Clang-Tidy") + +include(cmake-modules/build/ClangTidy) + +# ############################################################################## +# Cppcheck +# ############################################################################## + +set(USE_CPPCHECK + OFF + CACHE BOOL "Enable Cppcheck") +set(USE_CPPCHECK_SUPPRESSION_FILE + ${CMAKE_SOURCE_DIR}/.cppcheck-suppressions.txt + CACHE STRING + "Customize the path to the Cppcheck suppressions file of the project") + +include(cmake-modules/build/Cppcheck) + +# ############################################################################## +# CompilerFlags +# ############################################################################## + +include(cmake-modules/build/CompilerFlags) + +# ############################################################################## +# Hardening +# ############################################################################## + +set(USE_HARDENING_FLAGS + -D_GLIBCXX_ASSERTIONS # Enable assertions + -U_FORTIFY_SOURCE # Disable stack protector + -D_FORTIFY_SOURCE=3 # Enable stack protector + -fstack-protector-strong # Enable stack protector + -fcf-protection # Control Flow Guard + -fstack-clash-protection # Control Flow Guard + -Wimplicit-fallthrough # Enabled in compiler flags by default + -fstrict-flex-arrays=3 # Enable strict array bounds + -Wformat # Enabled in compiler flags by default + -Wformat=2 # Enabled in compiler flags by default + # -Wl,-z,nodlopen # Restrict dlopen(3) calls to shared objects + -Wl,-z,noexecstack # Enable data execution prevention by marking stack + # memory as non-executable + -Wl,-z,relro # Mark relocation table entries resolved at load-time as + # read-only + -Wl,-z,now # Mark relocation table entries resolved at load-time as + # read-only. It impacts startup performance + "-fsanitize=undefined -fsanitize-minimal-runtime" # Enable minimal runtime + # undefined behavior sanitizer + -fno-delete-null-pointer-checks + -fno-strict-overflow + -fno-strict-aliasing + -ftrivial-auto-var-init=zero + -Wtrampolines # Enable trampolines(gcc only) + -mbranch-protection=standard # Enable indirect branches(aarch64 only) + CACHE STRING "Additional hardening compilation flags for GCC/Clang") + +set(USE_HARDENING_LINKS + -fstack-protector-strong # Enable stack protector + "-fsanitize=undefined -fsanitize-minimal-runtime" # Enable minimal runtime + # undefined behavior sanitizer -Wl,-z,nodlopen # Restrict dlopen(3) calls to + # shared objects + -Wl,-z,noexecstack # Enable data execution prevention by marking stack + # memory as non-executable + -Wl,-z,relro # Mark relocation table entries resolved at load-time as + # read-only + -Wl,-z,now # Mark relocation table entries resolved at load-time as + # read-only. It impacts startup performance + CACHE STRING "Additional hardening linking flags for GCC/Clang") + +include(cmake-modules/build/Hardening) diff --git a/cmake/presets/arch/x64.json b/cmake/presets/arch/x64.json new file mode 100644 index 0000000..e7ea25b --- /dev/null +++ b/cmake/presets/arch/x64.json @@ -0,0 +1,20 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "x64", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "toolset": { + "value": "host=x64", + "strategy": "external" + }, + "hidden": true, + "cacheVariables": { + "VCPKG_TARGET_ARCHITECTURE": "x64" + } + } + ] +} diff --git a/cmake/presets/base.json b/cmake/presets/base.json new file mode 100644 index 0000000..f1b7667 --- /dev/null +++ b/cmake/presets/base.json @@ -0,0 +1,101 @@ +{ + "version": 6, + "include": [ + "generators/ninja.json", + "toolchains/vcpkg.json" + ], + "configurePresets": [ + { + "name": "base", + "hidden": true, + "inherits": [ + "ninja", + "vcpkg" + ], + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_EXPORT_COMPILE_COMMANDS": true, + "CMAKE_COMPILE_WARNING_AS_ERROR": false, + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_VERBOSE_MAKEFILE": "FALSE" + } + }, + { + "name": "base-config", + "hidden": true, + "inherits": [ + "ninja-config", + "vcpkg" + ], + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_EXPORT_COMPILE_COMMANDS": true, + "CMAKE_COMPILE_WARNING_AS_ERROR": false, + "CMAKE_VERBOSE_MAKEFILE": "FALSE" + } + } + ], + "buildPresets": [ + { + "name": "base", + "hidden": true, + "inherits": "ninja", + "configurePreset": "base" + }, + { + "name": "base-config-relwithdebinfo", + "hidden": true, + "inherits": "ninja-config-relwithdebinfo", + "configurePreset": "base-config" + }, + { + "name": "base-config-debug", + "hidden": true, + "inherits": "ninja-config-debug", + "configurePreset": "base-config" + } + ], + "testPresets": [ + { + "name": "base", + "hidden": true, + "inherits": "ninja", + "configurePreset": "base", + "output": { + "outputOnFailure": true + }, + "execution": { + "noTestsAction": "error", + "stopOnFailure": true + } + }, + { + "name": "base-config-relwithdebinfo", + "hidden": true, + "inherits": "ninja-config-relwithdebinfo", + "configurePreset": "base-config", + "output": { + "outputOnFailure": true + }, + "execution": { + "noTestsAction": "error", + "stopOnFailure": true + } + }, + { + "name": "base-config-debug", + "hidden": true, + "inherits": "ninja-config-debug", + "configurePreset": "base-config", + "output": { + "outputOnFailure": true + }, + "execution": { + "noTestsAction": "error", + "stopOnFailure": true + } + } + ] +} diff --git a/cmake/presets/compilers/gcc.json b/cmake/presets/compilers/gcc.json new file mode 100644 index 0000000..d920fb4 --- /dev/null +++ b/cmake/presets/compilers/gcc.json @@ -0,0 +1,13 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "gcc", + "hidden": true, + "cacheVariables": { + "CMAKE_C_COMPILER": "gcc", + "CMAKE_CXX_COMPILER": "g++" + } + } + ] +} diff --git a/cmake/presets/compilers/llvm.json b/cmake/presets/compilers/llvm.json new file mode 100644 index 0000000..5172325 --- /dev/null +++ b/cmake/presets/compilers/llvm.json @@ -0,0 +1,21 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "llvm", + "hidden": true, + "cacheVariables": { + "CMAKE_C_COMPILER": "clang", + "CMAKE_CXX_COMPILER": "clang++" + } + }, + { + "name": "llvm-cl", + "hidden": true, + "cacheVariables": { + "CMAKE_C_COMPILER": "clang-cl.exe", + "CMAKE_CXX_COMPILER": "clang-cl.exe" + } + } + ] +} diff --git a/cmake/presets/compilers/mingw.json b/cmake/presets/compilers/mingw.json new file mode 100644 index 0000000..302a898 --- /dev/null +++ b/cmake/presets/compilers/mingw.json @@ -0,0 +1,9 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "mingw", + "hidden": true + } + ] +} diff --git a/cmake/presets/compilers/msvc.json b/cmake/presets/compilers/msvc.json new file mode 100644 index 0000000..712dce1 --- /dev/null +++ b/cmake/presets/compilers/msvc.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "msvc", + "hidden": true, + "cacheVariables": { + "CMAKE_C_COMPILER": "cl", + "CMAKE_CXX_COMPILER": "cl" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + } + ] +} diff --git a/cmake/presets/default.json b/cmake/presets/default.json new file mode 100644 index 0000000..7423711 --- /dev/null +++ b/cmake/presets/default.json @@ -0,0 +1,54 @@ +{ + "version": 6, + "include": [ + "base.json" + ], + "configurePresets": [ + { + "name": "default", + "inherits": [ + "base" + ] + }, + { + "name": "default-config", + "inherits": [ + "base-config" + ] + } + ], + "buildPresets": [ + { + "name": "default", + "inherits": "base", + "configurePreset": "default" + }, + { + "name": "default-config-relwithdebinfo", + "inherits": "base-config-relwithdebinfo", + "configurePreset": "default-config" + }, + { + "name": "default-config-debug", + "inherits": "base-config-debug", + "configurePreset": "default-config" + } + ], + "testPresets": [ + { + "name": "default", + "inherits": "base", + "configurePreset": "default" + }, + { + "name": "default-config-relwithdebinfo", + "inherits": "base-config-relwithdebinfo", + "configurePreset": "default-config" + }, + { + "name": "default-config-debug", + "inherits": "base-config-debug", + "configurePreset": "default-config" + } + ] +} diff --git a/cmake/presets/generators/ninja.json b/cmake/presets/generators/ninja.json new file mode 100644 index 0000000..424407d --- /dev/null +++ b/cmake/presets/generators/ninja.json @@ -0,0 +1,53 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "ninja", + "hidden": true, + "generator": "Ninja" + }, + { + "name": "ninja-config", + "hidden": true, + "generator": "Ninja Multi-Config" + } + ], + "buildPresets": [ + { + "name": "ninja", + "hidden": true, + "configurePreset": "ninja" + }, + { + "name": "ninja-config-relwithdebinfo", + "hidden": true, + "configurePreset": "ninja-config", + "configuration": "RelWithDebInfo" + }, + { + "name": "ninja-config-debug", + "hidden": true, + "configurePreset": "ninja-config", + "configuration": "Debug" + } + ], + "testPresets": [ + { + "name": "ninja", + "hidden": true, + "configurePreset": "ninja" + }, + { + "name": "ninja-config-relwithdebinfo", + "hidden": true, + "configurePreset": "ninja-config", + "configuration": "RelWithDebInfo" + }, + { + "name": "ninja-config-debug", + "hidden": true, + "configurePreset": "ninja-config", + "configuration": "Debug" + } + ] +} diff --git a/cmake/presets/os/darwin.json b/cmake/presets/os/darwin.json new file mode 100644 index 0000000..e3f3fbe --- /dev/null +++ b/cmake/presets/os/darwin.json @@ -0,0 +1,28 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "darwin", + "hidden": true, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + } + } + ], + "buildPresets": [ + { + "name": "darwin", + "hidden": true, + "configurePreset": "darwin" + } + ], + "testPresets": [ + { + "name": "darwin", + "hidden": true, + "configurePreset": "darwin" + } + ] +} diff --git a/cmake/presets/os/linux.json b/cmake/presets/os/linux.json new file mode 100644 index 0000000..684e078 --- /dev/null +++ b/cmake/presets/os/linux.json @@ -0,0 +1,28 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "linux", + "hidden": true, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + } + } + ], + "buildPresets": [ + { + "name": "linux", + "hidden": true, + "configurePreset": "linux" + } + ], + "testPresets": [ + { + "name": "linux", + "hidden": true, + "configurePreset": "linux" + } + ] +} diff --git a/cmake/presets/os/windows.json b/cmake/presets/os/windows.json new file mode 100644 index 0000000..9f78bad --- /dev/null +++ b/cmake/presets/os/windows.json @@ -0,0 +1,28 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "windows", + "hidden": true, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + } + ], + "buildPresets": [ + { + "name": "windows", + "hidden": true, + "configurePreset": "windows" + } + ], + "testPresets": [ + { + "name": "windows", + "hidden": true, + "configurePreset": "windows" + } + ] +} diff --git a/cmake/presets/toolchains/vcpkg.json b/cmake/presets/toolchains/vcpkg.json new file mode 100644 index 0000000..3e8ec67 --- /dev/null +++ b/cmake/presets/toolchains/vcpkg.json @@ -0,0 +1,16 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "vcpkg", + "hidden": true, + "toolchainFile": "${sourceDir}/cmake/vcpkg/vcpkg.toolchain.cmake", + "cacheVariables": { + "VCPKG_INSTALL_OPTIONS": "--no-print-usage", + "VCPKG_MANIFEST_FEATURES": "test", + "VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/cmake/vcpkg/triplets", + "VCPKG_OVERLAY_PORTS": "${sourceDir}/cmake/vcpkg/ports" + } + } + ] +} diff --git a/cmake/presets/triplets/x64-linux.json b/cmake/presets/triplets/x64-linux.json new file mode 100644 index 0000000..dcbccbd --- /dev/null +++ b/cmake/presets/triplets/x64-linux.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "include": [ + "../arch/x64.json" + ], + "configurePresets": [ + { + "name": "x64-linux", + "hidden": true, + "inherits": [ + "x64" + ], + "cacheVariables": { + "VCPKG_TARGET_TRIPLET": "x64-linux" + } + } + ] +} diff --git a/cmake/presets/triplets/x64-mingw-dynamic.json b/cmake/presets/triplets/x64-mingw-dynamic.json new file mode 100644 index 0000000..672dde6 --- /dev/null +++ b/cmake/presets/triplets/x64-mingw-dynamic.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "include": [ + "../arch/x64.json" + ], + "configurePresets": [ + { + "name": "x64-mingw-dynamic", + "hidden": true, + "inherits": [ + "x64" + ], + "cacheVariables": { + "VCPKG_TARGET_TRIPLET": "x64-mingw-dynamic" + } + } + ] +} diff --git a/cmake/presets/triplets/x64-mingw-static.json b/cmake/presets/triplets/x64-mingw-static.json new file mode 100644 index 0000000..49fb429 --- /dev/null +++ b/cmake/presets/triplets/x64-mingw-static.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "include": [ + "../arch/x64.json" + ], + "configurePresets": [ + { + "name": "x64-mingw-static", + "hidden": true, + "inherits": [ + "x64" + ], + "cacheVariables": { + "VCPKG_TARGET_TRIPLET": "x64-mingw-static" + } + } + ] +} diff --git a/cmake/presets/triplets/x64-osx.json b/cmake/presets/triplets/x64-osx.json new file mode 100644 index 0000000..bc0c0d3 --- /dev/null +++ b/cmake/presets/triplets/x64-osx.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "include": [ + "../arch/x64.json" + ], + "configurePresets": [ + { + "name": "x64-osx", + "hidden": true, + "inherits": [ + "x64" + ], + "cacheVariables": { + "VCPKG_TARGET_TRIPLET": "x64-osx" + } + } + ] +} diff --git a/cmake/presets/triplets/x64-windows.json b/cmake/presets/triplets/x64-windows.json new file mode 100644 index 0000000..929700c --- /dev/null +++ b/cmake/presets/triplets/x64-windows.json @@ -0,0 +1,18 @@ +{ + "version": 6, + "include": [ + "../arch/x64.json" + ], + "configurePresets": [ + { + "name": "x64-windows", + "hidden": true, + "inherits": [ + "x64" + ], + "cacheVariables": { + "VCPKG_TARGET_TRIPLET": "x64-windows" + } + } + ] +} diff --git a/cmake/presets/x64-linux-gcc.json b/cmake/presets/x64-linux-gcc.json new file mode 100644 index 0000000..5971ffc --- /dev/null +++ b/cmake/presets/x64-linux-gcc.json @@ -0,0 +1,34 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/linux.json", + "compilers/gcc.json", + "triplets/x64-linux.json" + ], + "configurePresets": [ + { + "name": "x64-linux-gcc", + "inherits": [ + "base", + "linux", + "gcc", + "x64-linux" + ] + } + ], + "buildPresets": [ + { + "name": "x64-linux-gcc", + "inherits": "base", + "configurePreset": "x64-linux-gcc" + } + ], + "testPresets": [ + { + "name": "x64-linux-gcc", + "inherits": "base", + "configurePreset": "x64-linux-gcc" + } + ] +} diff --git a/cmake/presets/x64-linux-llvm.json b/cmake/presets/x64-linux-llvm.json new file mode 100644 index 0000000..9fbc624 --- /dev/null +++ b/cmake/presets/x64-linux-llvm.json @@ -0,0 +1,34 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/linux.json", + "compilers/llvm.json", + "triplets/x64-linux.json" + ], + "configurePresets": [ + { + "name": "x64-linux-llvm", + "inherits": [ + "base", + "linux", + "llvm", + "x64-linux" + ] + } + ], + "buildPresets": [ + { + "name": "x64-linux-llvm", + "inherits": "base", + "configurePreset": "x64-linux-llvm" + } + ], + "testPresets": [ + { + "name": "x64-linux-llvm", + "inherits": "base", + "configurePreset": "x64-linux-llvm" + } + ] +} diff --git a/cmake/presets/x64-mingw-dynamic-windows-mingw.json b/cmake/presets/x64-mingw-dynamic-windows-mingw.json new file mode 100644 index 0000000..1300509 --- /dev/null +++ b/cmake/presets/x64-mingw-dynamic-windows-mingw.json @@ -0,0 +1,35 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/windows.json", + "compilers/mingw.json", + "triplets/x64-mingw-dynamic.json" + ], + "configurePresets": [ + { + "name": "x64-mingw-dynamic-windows-mingw", + "inherits": [ + "base", + "windows", + "mingw", + "x64-mingw-dynamic" + ] + + } + ], + "buildPresets": [ + { + "name": "x64-mingw-dynamic-windows-mingw", + "inherits": "base", + "configurePreset": "x64-mingw-dynamic-windows-mingw" + } + ], + "testPresets": [ + { + "name": "x64-mingw-dynamic-windows-mingw", + "inherits": "base", + "configurePreset": "x64-mingw-dynamic-windows-mingw" + } + ] +} diff --git a/cmake/presets/x64-mingw-static-windows-mingw.json b/cmake/presets/x64-mingw-static-windows-mingw.json new file mode 100644 index 0000000..a21a0c2 --- /dev/null +++ b/cmake/presets/x64-mingw-static-windows-mingw.json @@ -0,0 +1,34 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/windows.json", + "compilers/mingw.json", + "triplets/x64-mingw-static.json" + ], + "configurePresets": [ + { + "name": "x64-mingw-static-windows-mingw", + "inherits": [ + "base", + "windows", + "mingw", + "x64-mingw-static" + ] + } + ], + "buildPresets": [ + { + "name": "x64-mingw-static-windows-mingw", + "inherits": "base", + "configurePreset": "x64-mingw-static-windows-mingw" + } + ], + "testPresets": [ + { + "name": "x64-mingw-static-windows-mingw", + "inherits": "base", + "configurePreset": "x64-mingw-static-windows-mingw" + } + ] +} diff --git a/cmake/presets/x64-osx-gcc.json b/cmake/presets/x64-osx-gcc.json new file mode 100644 index 0000000..405af6c --- /dev/null +++ b/cmake/presets/x64-osx-gcc.json @@ -0,0 +1,53 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/darwin.json", + "compilers/gcc.json", + "triplets/x64-osx.json" + ], + "configurePresets": [ + { + "name": "x64-osx-gcc", + "inherits": [ + "base", + "darwin", + "gcc", + "x64-osx" + ] + } + ], + "buildPresets": [ + { + "name": "x64-osx-gcc", + "inherits": "base", + "configurePreset": "x64-osx-gcc" + } + ], + "testPresets": [ + { + "name": "x64-osx-gcc", + "inherits": "base", + "configurePreset": "x64-osx-gcc" + } + ], + "workflowPresets": [ + { + "name": "x64-osx-gcc", + "steps": [ + { + "type": "configure", + "name": "x64-osx-gcc" + }, + { + "type": "build", + "name": "x64-osx-gcc" + }, + { + "type": "test", + "name": "x64-osx-gcc" + } + ] + } + ] +} diff --git a/cmake/presets/x64-osx-llvm.json b/cmake/presets/x64-osx-llvm.json new file mode 100644 index 0000000..fc43d06 --- /dev/null +++ b/cmake/presets/x64-osx-llvm.json @@ -0,0 +1,53 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/darwin.json", + "compilers/llvm.json", + "triplets/x64-osx.json" + ], + "configurePresets": [ + { + "name": "x64-osx-llvm", + "inherits": [ + "base", + "darwin", + "llvm", + "x64-osx" + ] + } + ], + "buildPresets": [ + { + "name": "x64-osx-llvm", + "inherits": "base", + "configurePreset": "x64-osx-llvm" + } + ], + "testPresets": [ + { + "name": "x64-osx-llvm", + "inherits": "base", + "configurePreset": "x64-osx-llvm" + } + ], + "workflowPresets": [ + { + "name": "x64-osx-llvm", + "steps": [ + { + "type": "configure", + "name": "x64-osx-llvm" + }, + { + "type": "build", + "name": "x64-osx-llvm" + }, + { + "type": "test", + "name": "x64-osx-llvm" + } + ] + } + ] +} diff --git a/cmake/presets/x64-windows-llvm.json b/cmake/presets/x64-windows-llvm.json new file mode 100644 index 0000000..04acc62 --- /dev/null +++ b/cmake/presets/x64-windows-llvm.json @@ -0,0 +1,56 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/windows.json", + "compilers/llvm.json", + "triplets/x64-windows.json" + ], + "configurePresets": [ + { + "name": "x64-windows-llvm", + "inherits": [ + "base", + "windows", + "llvm-cl", + "x64-windows" + ], + "cacheVariables": { + "USE_SANITIZER": "OFF" + } + } + ], + "buildPresets": [ + { + "name": "x64-windows-llvm", + "inherits": "base", + "configurePreset": "x64-windows-llvm" + } + ], + "testPresets": [ + { + "name": "x64-windows-llvm", + "inherits": "base", + "configurePreset": "x64-windows-llvm" + } + ], + "workflowPresets": [ + { + "name": "x64-windows-llvm", + "steps": [ + { + "type": "configure", + "name": "x64-windows-llvm" + }, + { + "type": "build", + "name": "x64-windows-llvm" + }, + { + "type": "test", + "name": "x64-windows-llvm" + } + ] + } + ] +} diff --git a/cmake/presets/x64-windows-msvc.json b/cmake/presets/x64-windows-msvc.json new file mode 100644 index 0000000..f63a964 --- /dev/null +++ b/cmake/presets/x64-windows-msvc.json @@ -0,0 +1,53 @@ +{ + "version": 6, + "include": [ + "base.json", + "os/windows.json", + "compilers/msvc.json", + "triplets/x64-windows.json" + ], + "configurePresets": [ + { + "name": "x64-windows-msvc", + "inherits": [ + "base", + "windows", + "msvc", + "x64-windows" + ] + } + ], + "buildPresets": [ + { + "name": "x64-windows-msvc", + "inherits": "base", + "configurePreset": "x64-windows-msvc" + } + ], + "testPresets": [ + { + "name": "x64-windows-msvc", + "inherits": "base", + "configurePreset": "x64-windows-msvc" + } + ], + "workflowPresets": [ + { + "name": "x64-windows-msvc", + "steps": [ + { + "type": "configure", + "name": "x64-windows-msvc" + }, + { + "type": "build", + "name": "x64-windows-msvc" + }, + { + "type": "test", + "name": "x64-windows-msvc" + } + ] + } + ] +} diff --git a/cmake/vcpkg/bootstrap/vcpkg-config.cmake b/cmake/vcpkg/bootstrap/vcpkg-config.cmake new file mode 100644 index 0000000..869092b --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg-config.cmake @@ -0,0 +1,20 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2023 Mikhail Svetkin +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +include_guard(GLOBAL) + +# CMAKE_HOST_* variables are not available during first configure on windows. +cmake_host_system_information(RESULT __vcpkg_bootstrap_host QUERY OS_NAME) +cmake_host_system_information(RESULT __vcpkg_bootstrap_arch QUERY OS_PLATFORM) + +set(__vcpkg_bootstrap_list_dir "${CMAKE_CURRENT_LIST_DIR}") + +include(${__vcpkg_bootstrap_list_dir}/vcpkg_skip_install_on_reconfigure.cmake) +include(${__vcpkg_bootstrap_list_dir}/vcpkg_bootstrap.cmake) +include(${__vcpkg_bootstrap_list_dir}/vcpkg_chainload_toolchain.cmake) +include(${__vcpkg_bootstrap_list_dir}/vcpkg_load_triplet.cmake) +include(${__vcpkg_bootstrap_list_dir}/vcpkg_export_mode.cmake) +include(${__vcpkg_bootstrap_list_dir}/vcpkg_configure.cmake) diff --git a/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake b/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake new file mode 100644 index 0000000..c697a0c --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_bootstrap.cmake @@ -0,0 +1,209 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2023 Mikhail Svetkin +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +# stash all local changes +function(_vcpkg_stash vcpkg_root) + message(STATUS "vcpkg stash all local changes") + + execute_process( + COMMAND ${GIT_EXECUTABLE} stash + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message(FATAL_ERROR "${GIT_EXECUTABLE} stash failed with ${result}") + endif() +endfunction() + +# checkout to a specific baseline +function(_vcpkg_checkout vcpkg_root vcpkg_ref) + message(STATUS "vcpkg checkout to ${vcpkg_ref}") + + if(EXISTS "${vcpkg_root}/.git/shallow") + message( + WARNING + "vcpkg is shallow now and unshallow to retrieve the Git tree object hash for a specific version port......" + ) + execute_process( + COMMAND ${GIT_EXECUTABLE} fetch --unshallow + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message( + FATAL_ERROR "${GIT_EXECUTABLE} fetch --unshallow failed with ${result}") + endif() + endif() + + execute_process( + COMMAND ${GIT_EXECUTABLE} checkout ${vcpkg_ref} + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message( + FATAL_ERROR + "${GIT_EXECUTABLE} checkout ${vcpkg_ref} failed with ${result}") + endif() +endfunction() + +# clone +function(_vcpkg_clone vcpkg_root vcpkg_repo vcpkg_ref) + execute_process( + COMMAND ${GIT_EXECUTABLE} clone ${vcpkg_repo} ${vcpkg_root} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message(FATAL_ERROR "failed to clone ${vcpkg_repo} to ${vcpkg_root}") + endif() + + file(LOCK "${vcpkg_root}" DIRECTORY) + _vcpkg_checkout(${vcpkg_root} ${vcpkg_ref}) +endfunction() + +# bootstrap +function(_vcpkg_tool_bootstrap vcpkg_root) + message(STATUS "Bootstrap vckpg tool") + + if("${__vcpkg_bootstrap_host}" STREQUAL "Windows") + set(bootstrap_cmd "${vcpkg_root}/bootstrap-vcpkg.bat") + else() + set(bootstrap_cmd "${vcpkg_root}/bootstrap-vcpkg.sh") + endif() + + execute_process( + COMMAND ${bootstrap_cmd} -disableMetrics + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + if(CMAKE_HOST_UNIX) + message(STATUS "Retry to build vcpkg from source...") + set(bootstrap_impl "${vcpkg_root}/scripts/bootstrap.sh") + file(READ "${bootstrap_impl}" file_contents) + string(REPLACE [[elif [ "$ARCH" = "x86_64" ]; then]] + [[elif [ "$ARCH" = "" ]; then]] file_contents + "${file_contents}") + file(WRITE "${bootstrap_impl}" "${file_contents}") + + execute_process( + COMMAND ${bootstrap_cmd} -disableMetrics + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + endif() + + if(NOT result EQUAL "0") + message(FATAL_ERROR "${bootstrap_cmd} failed with ${result}") + endif() + endif() +endfunction() + +# upgrade +function(_vcpkg_upgrade vcpkg_root vcpkg_repo vcpkg_ref) + file(LOCK "${vcpkg_root}" DIRECTORY) + + if(NOT EXISTS "${vcpkg_root}/.git") + message(WARNING "vcpkg not found in git work tree, skipping upgrade") + return() + endif() + + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + WORKING_DIRECTORY ${vcpkg_root} + OUTPUT_VARIABLE current_git_hash + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message( + FATAL_ERROR "${GIT_EXECUTABLE} rev-parse HEAD failed with ${result}") + endif() + + if("${current_git_hash}" STREQUAL "${vcpkg_ref}") + return() + endif() + + message(STATUS "Upgrade vcpkg") + message(STATUS "vcpkg current commit: ${current_git_hash}") + message(STATUS "vcpkg target commit: ${vcpkg_ref}") + + execute_process( + COMMAND ${GIT_EXECUTABLE} remote set-url origin ${vcpkg_repo} + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message(FATAL_ERROR "failed to change origin to ${vcpkg_repo}") + endif() + + execute_process( + COMMAND ${GIT_EXECUTABLE} fetch + WORKING_DIRECTORY ${vcpkg_root} + RESULT_VARIABLE result) + + if(NOT result EQUAL "0") + message(FATAL_ERROR "${GIT_EXECUTABLE} fetch failed with ${result}") + endif() + + _vcpkg_stash(${vcpkg_root}) + _vcpkg_checkout(${vcpkg_root} ${vcpkg_ref}) + _vcpkg_tool_bootstrap(${vcpkg_root}) +endfunction() + +# find root +function(_vcpkg_find_root cache_dir_name out_vcpkg_root) + if("${__vcpkg_bootstrap_host}" STREQUAL "Windows") + set(root "$ENV{LOCALAPPDATA}/vcpkg/projects/${cache_dir_name}/cache") + else() + set(root "$ENV{HOME}/.cache/vcpkg/projects/${cache_dir_name}") + endif() + + set(${out_vcpkg_root} + ${root} + PARENT_SCOPE) +endfunction() + +# set vcpkg_root/toolchain_file cache variables +function(_vcpkg_set_cache_variables vcpkg_root) + set(_VCPKG_ROOT + "${vcpkg_root}" + CACHE INTERNAL "vcpkg root") + + set(_VCPKG_TOOLCHAIN_FILE + "${vcpkg_root}/scripts/buildsystems/vcpkg.cmake" + CACHE INTERNAL "vcpkg toolchain file") +endfunction() + +# bootstrap +function(_vcpkg_bootstrap) + cmake_parse_arguments(PARSE_ARGV 0 "arg" "" "CACHE_DIR_NAME;REPO;REF" "") + + if(DEFINED arg_UNPARSED_ARGUMENTS) + message( + FATAL_ERROR "internal error: ${CMAKE_CURRENT_FUNCTION} passed extra args:" + "${arg_UNPARSED_ARGUMENTS}") + endif() + + find_package(Git QUIET REQUIRED) + + if(DEFINED CACHE{_VCPKG_ROOT}) + set(vcpkg_root $CACHE{_VCPKG_ROOT}) + else() + _vcpkg_find_root("${arg_CACHE_DIR_NAME}" vcpkg_root) + endif() + + if(NOT EXISTS ${vcpkg_root}) + message(STATUS "Setup vcpkg") + _vcpkg_clone(${vcpkg_root} ${arg_REPO} ${arg_REF}) + _vcpkg_tool_bootstrap(${vcpkg_root}) + else() + message(STATUS "Found vcpkg in: ${vcpkg_root}") + _vcpkg_upgrade(${vcpkg_root} ${arg_REPO} ${arg_REF}) + endif() + + _vcpkg_set_cache_variables("${vcpkg_root}") +endfunction() diff --git a/cmake/vcpkg/bootstrap/vcpkg_chainload_toolchain.cmake b/cmake/vcpkg/bootstrap/vcpkg_chainload_toolchain.cmake new file mode 100644 index 0000000..2cff74e --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_chainload_toolchain.cmake @@ -0,0 +1,45 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +# configure VCPKG_CHAINLOAD_TOOLCHAIN_FILE based on VCPKG_TARGET_TRIPLET and +# VCPKG_TARGET_ARCHITECTURE +function(_vcpkg_chainload_toolchain) + if(DEFINED VCPKG_CHAINLOAD_TOOLCHAIN_FILE) + return() + endif() + + if(NOT DEFINED VCPKG_TARGET_TRIPLET OR NOT DEFINED VCPKG_TARGET_ARCHITECTURE) + message( + FATAL_ERROR + "VCPKG_TARGET_TRIPLET and VCPKG_TARGET_ARCHITECTURE must be set before calling _vcpkg_chainload_toolchain()" + ) + endif() + + # Load toolchain variables from triplet and architecture + + string(LENGTH "${VCPKG_TARGET_ARCHITECTURE}-" _prefix_len) + string(SUBSTRING ${VCPKG_TARGET_TRIPLET} ${_prefix_len} -1 _stripped_string) + string(REPLACE "-" ";" _triplet_parts "${_stripped_string}") + list(GET _triplet_parts 0 _chainload_toolchain_name) + + set(_toolchain "scripts/toolchains/${_chainload_toolchain_name}.cmake") + + if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${_toolchain}") + set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE + "${CMAKE_CURRENT_LIST_DIR}/${_toolchain}" + CACHE INTERNAL "vcpkg chainload") + elseif(EXISTS "${_VCPKG_ROOT}/${_toolchain}") + set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE + "${_VCPKG_ROOT}/${_toolchain}" + CACHE INTERNAL "vcpkg chainload") + else() + message( + WARNING + "Could not find toolchain file for ${_chainload_toolchain_name}, skipping chainload" + ) + endif() + message( + STATUS "vcpkg_chainload_toolchain_file: ${VCPKG_CHAINLOAD_TOOLCHAIN_FILE}") +endfunction() diff --git a/cmake/vcpkg/bootstrap/vcpkg_configure.cmake b/cmake/vcpkg/bootstrap/vcpkg_configure.cmake new file mode 100644 index 0000000..3577557 --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_configure.cmake @@ -0,0 +1,100 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2023 Mikhail Svetkin +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +function(_vcpkg_export_mode) + if(_VCPKG_ROOT) + _vcpkg_bootstrap(${ARGN}) + _vcpkg_skip_install_on_reconfigure() + endif() + + set(_export_export_dir "${CMAKE_SOURCE_DIR}/cmake/vcpkg/export") + set(_export_vcpkg_root "${_export_export_dir}/${VCPKG_TARGET_TRIPLET}") + _vcpkg_setup_export("${_export_export_dir}" "${_export_vcpkg_root}") + + # Set vcpkg root and toolchian file variables to target the exported artifacts + if(NOT EXISTS "${_export_vcpkg_root}") + message( + FATAL_ERROR + "vcpkg export not found for triplet ${VCPKG_TARGET_TRIPLET}, check vcpkg logs for details" + ) + endif() + + message( + STATUS "Using vcpkg exported artifacts as vcpkg root: ${_export_vcpkg_root}" + ) + + _vcpkg_set_cache_variables("${_export_vcpkg_root}") + + # Disable vcpkg.cmake to read the cache variable to check if it should install + # packages + set(VCPKG_MANIFEST_DIR + "" + CACHE PATH "vcpkg manifest dir" FORCE) + message(STATUS "Disable manifest mode for using vcpkg exported artifacts") +endfunction() + +macro(detect_vcpkg) + # Respect environment variable VCPKG_ROOT and VCPKG_INSTALLATION_ROOT if set + if(DEFINED ENV{VCPKG_ROOT} AND NOT "$ENV{VCPKG_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + elseif(DEFINED ENV{VCPKG_INSTALLATION_ROOT} + AND NOT "$ENV{VCPKG_INSTALLATION_ROOT}" STREQUAL "") + set(_VCPKG_ROOT + "$ENV{VCPKG_INSTALLATION_ROOT}" + CACHE PATH "Vcpkg root directory" FORCE) + else() + unset(_VCPKG_ROOT CACHE) + endif() +endmacro() + +# Add VCPKG_INSTALL_REPORT_FAILURE option to report vcpkg failure in detail +function(_vcpkg_install_report_failure) + if(DEFINED ENV{CI} AND NOT "$ENV{CI}" STREQUAL "") + set(VCPKG_INSTALL_REPORT_FAILURE + ON + CACHE INTERNAL "Enable vcpkg install failure report in detail") + endif() + file(READ "$CACHE{_VCPKG_TOOLCHAIN_FILE}" _vcpkg_toolchain_content) + if(VCPKG_INSTALL_REPORT_FAILURE + AND NOT "${_vcpkg_toolchain_content}" MATCHES + [[VCPKG INSTALL REPORT FAILURE IN DETAIL]]) + string( + REPLACE + [[message(STATUS "Running vcpkg install - failed")]] + [[message(STATUS "Running vcpkg install - failed") + file(READ "${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/vcpkg/issue_body.md" issue_body_content) + message(STATUS "") + set(Z_NATIVE_VCPKG_MANIFEST_INSTALL_LOGFILE "${Z_NATIVE_VCPKG_MANIFEST_INSTALL_LOGFILE}\nVCPKG INSTALL REPORT FAILURE IN DETAIL: ${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/vcpkg/issue_body.md\n${issue_body_content}\n")]] + _vcpkg_toolchain_content + "${_vcpkg_toolchain_content}") + file(WRITE "$CACHE{_VCPKG_TOOLCHAIN_FILE}" "${_vcpkg_toolchain_content}") + endif() +endfunction() + +# bootstrap and configure vcpkg +macro(vcpkg_configure) + detect_vcpkg() + + if(NOT VCPKG_EXPORT_MODE) + _vcpkg_bootstrap(${ARGN}) + _vcpkg_skip_install_on_reconfigure() + endif() + + _vcpkg_load_triplet() + + if(VCPKG_EXPORT_MODE) + _vcpkg_export_mode(${ARGN}) + endif() + + _vcpkg_chainload_toolchain() + + _vcpkg_install_report_failure() + + message(STATUS "vcpkg_toolchain_file:$CACHE{_VCPKG_TOOLCHAIN_FILE}") + include("$CACHE{_VCPKG_TOOLCHAIN_FILE}") +endmacro() diff --git a/cmake/vcpkg/bootstrap/vcpkg_export_mode.cmake b/cmake/vcpkg/bootstrap/vcpkg_export_mode.cmake new file mode 100644 index 0000000..2b6e0b9 --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_export_mode.cmake @@ -0,0 +1,97 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +# This function using vcpkg install and export to export vcpkg artifacts to +# ${CMAKE_SOURCE_DIR}/cmake/vcpkg/export +function(_vcpkg_setup_export export_export_dir export_vcpkg_root) + set(_export_install_dir + "${CMAKE_SOURCE_DIR}/out/vcpkg_installed/${VCPKG_TARGET_TRIPLET}") + + # Update exported artifacts when _VCPKG_ROOT is found + if(DEFINED _VCPKG_ROOT AND NOT ${export_vcpkg_root} STREQUAL _VCPKG_ROOT) + message( + STATUS + "Exporting vcpkg to ${export_vcpkg_root} when VCPKG_EXPORT_MODE is enabled" + ) + + if("${__vcpkg_bootstrap_host}" STREQUAL "Windows") + set(vcpkg_cmd "$CACHE{_VCPKG_ROOT}/vcpkg.exe") + else() + set(vcpkg_cmd "$CACHE{_VCPKG_ROOT}/vcpkg") + endif() + + if(NOT EXISTS ${vcpkg_cmd}) + message( + FATAL_ERROR + "vcpkg executable not found when export_with_manifest enabled: ${vcpkg_cmd}" + ) + endif() + + if(DEFINED VCPKG_FEATURE_FLAGS OR DEFINED CACHE{VCPKG_FEATURE_FLAGS}) + list(JOIN VCPKG_FEATURE_FLAGS "," vcpkg_feature_flags) + set(vcpkg_feature_flags "--feature-flags=${vcpkg_feature_flags}") + endif() + + foreach(_vcpkg_feature IN LISTS VCPKG_MANIFEST_FEATURES) + list(APPEND vcpkg_additional_manifest_params + "--x-feature=${_vcpkg_feature}") + endforeach() + + if(VCPKG_MANIFEST_NO_DEFAULT_FEATURES) + list(APPEND vcpkg_additional_manifest_params "--x-no-default-features") + endif() + + if(DEFINED VCPKG_HOST_TRIPLET AND NOT VCPKG_HOST_TRIPLET STREQUAL "") + list(APPEND vcpkg_additional_manifest_params + "--host-triplet=${VCPKG_HOST_TRIPLET}") + endif() + + set(_additional_args + --vcpkg-root + ${_VCPKG_ROOT} + --overlay-ports + "${VCPKG_OVERLAY_PORTS}" + --overlay-triplets + "${VCPKG_OVERLAY_TRIPLETS}" + --triplet + ${VCPKG_TARGET_TRIPLET} + --x-install-root + "${_export_install_dir}") + + execute_process( + COMMAND + ${vcpkg_cmd} install "--x-wait-for-lock" + "--x-manifest-root=${CMAKE_SOURCE_DIR}" ${_additional_args} + ${vcpkg_feature_flags} ${vcpkg_additional_manifest_params} + ${VCPKG_INSTALL_OPTIONS} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + RESULT_VARIABLE install_packages_result) + + if(NOT install_packages_result EQUAL 0) + message( + FATAL_ERROR + "vcpkg install failed with ${install_packages_result}, check vcpkg logs for details" + ) + endif() + + execute_process( + COMMAND ${vcpkg_cmd} export --raw --output ${VCPKG_TARGET_TRIPLET} + --output-dir ${export_export_dir} ${_additional_args} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + RESULT_VARIABLE export_packages_result) + + if(NOT export_packages_result EQUAL 0) + message( + FATAL_ERROR + "vcpkg export failed with ${export_packages_result}, check vcpkg logs for details" + ) + endif() + + file(COPY "${_VCPKG_ROOT}/triplets" DESTINATION "${export_vcpkg_root}") + + file(COPY "${_VCPKG_ROOT}/scripts/toolchains" + DESTINATION "${export_vcpkg_root}/scripts") + endif() +endfunction() diff --git a/cmake/vcpkg/bootstrap/vcpkg_load_triplet.cmake b/cmake/vcpkg/bootstrap/vcpkg_load_triplet.cmake new file mode 100644 index 0000000..6ceb2f0 --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_load_triplet.cmake @@ -0,0 +1,225 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +# copied from vcpkg vcpkg.cmake +function(_vcpkg_detect_host_triplet) + if(DEFINED VCPKG_TARGET_TRIPLET) + return() + endif() + + if(CMAKE_GENERATOR_PLATFORM MATCHES "^[Ww][Ii][Nn]32$") + set(_detect_target_triplet_arch x86) + elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Xx]64$") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Aa][Rr][Mm]$") + set(_detect_target_triplet_arch arm) + elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Aa][Rr][Mm]64$") + set(_detect_target_triplet_arch arm64) + else() + if(CMAKE_GENERATOR STREQUAL "Visual Studio 14 2015 Win64") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 14 2015 ARM") + set(_detect_target_triplet_arch arm) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 14 2015") + set(_detect_target_triplet_arch x86) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 15 2017 Win64") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 15 2017 ARM") + set(_detect_target_triplet_arch arm) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 15 2017") + set(_detect_target_triplet_arch x86) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 16 2019" + AND CMAKE_VS_PLATFORM_NAME_DEFAULT STREQUAL "ARM64") + set(_detect_target_triplet_arch arm64) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 16 2019") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 17 2022" + AND CMAKE_VS_PLATFORM_NAME_DEFAULT STREQUAL "ARM64") + set(_detect_target_triplet_arch arm64) + elseif(CMAKE_GENERATOR STREQUAL "Visual Studio 17 2022") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND DEFINED + CMAKE_OSX_ARCHITECTURES) + list(LENGTH CMAKE_OSX_ARCHITECTURES _detect_osx_arch_count) + if(_detect_osx_arch_count EQUAL "0") + message( + WARNING + "Unable to determine target architecture from ${CMAKE_OSX_ARCHITECTURES}. " + "Consider providing a value for the CMAKE_OSX_ARCHITECTURES cache variable. " + "Continuing without vcpkg.") + set(VCPKG_TOOLCHAIN ON) + cmake_policy(POP) + return() + endif() + + if(_detect_osx_arch_count GREATER "1") + message( + WARNING + "Detected more than one target architecture from ${CMAKE_OSX_ARCHITECTURES}. Using the first one." + ) + endif() + list(GET CMAKE_OSX_ARCHITECTURES "0" _detect_osx_target_arch) + if(_detect_osx_target_arch STREQUAL "arm64") + set(_detect_target_triplet_arch arm64) + elseif(_detect_osx_target_arch STREQUAL "arm64s") + set(_detect_target_triplet_arch arm64s) + elseif(_detect_osx_target_arch STREQUAL "armv7s") + set(_detect_target_triplet_arch armv7s) + elseif(_detect_osx_target_arch STREQUAL "armv7") + set(_detect_target_triplet_arch arm) + elseif(_detect_osx_target_arch STREQUAL "x86_64") + set(_detect_target_triplet_arch x64) + elseif(_detect_osx_target_arch STREQUAL "i386") + set(_detect_target_triplet_arch x86) + else() + message( + WARNING + "Unable to determine target architecture from ${CMAKE_OSX_ARCHITECTURES}, continuing without vcpkg." + ) + set(VCPKG_TOOLCHAIN ON) + cmake_policy(POP) + return() + endif() + else() + find_program(_vcpkg_cl cl) + if(_vcpkg_cl MATCHES "amd64/cl.exe$" OR _vcpkg_cl MATCHES "x64/cl.exe$") + set(_detect_target_triplet_arch x64) + elseif(_vcpkg_cl MATCHES "arm/cl.exe$") + set(_detect_target_triplet_arch arm) + elseif(_vcpkg_cl MATCHES "arm64/cl.exe$") + set(_detect_target_triplet_arch arm64) + elseif(_vcpkg_cl MATCHES "bin/cl.exe$" OR _vcpkg_cl MATCHES "x86/cl.exe$") + set(_detect_target_triplet_arch x86) + elseif( + CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64" + OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64" + OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "amd64") + set(_detect_target_triplet_arch x64) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^i.86$") + set(_detect_target_triplet_arch x86) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "s390x") + set(_detect_target_triplet_arch s390x) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "ppc64le") + set(_detect_target_triplet_arch ppc64le) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "armv7l") + set(_detect_target_triplet_arch arm) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)$") + set(_detect_target_triplet_arch arm64) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "riscv32") + set(_detect_target_triplet_arch riscv32) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "riscv64") + set(_detect_target_triplet_arch riscv64) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "loongarch32") + set(_detect_target_triplet_arch loongarch32) + elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "loongarch64") + set(_detect_target_triplet_arch loongarch64) + else() + message( + WARNING + "Unable to determine target architecture from ${CMAKE_HOST_SYSTEM_PROCESSOR}, continuing without vcpkg." + ) + set(VCPKG_TOOLCHAIN ON) + cmake_policy(POP) + return() + endif() + endif() + endif() + + # detect the target platform + if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" OR CMAKE_SYSTEM_NAME STREQUAL + "WindowsPhone") + set(_detect_target_triplet_plat uwp) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux" + OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")) + set(_detect_target_triplet_plat linux) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin" + OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" + )) + set(_detect_target_triplet_plat osx) + elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS") + set(_detect_target_triplet_plat ios) + elseif(MINGW) + set(_detect_target_triplet_plat mingw-dynamic) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" + OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" + )) + if(XBOX_CONSOLE_TARGET STREQUAL "scarlett") + set(_detect_target_triplet_plat xbox-scarlett) + elseif(XBOX_CONSOLE_TARGET STREQUAL "xboxone") + set(_detect_target_triplet_plat xbox-xboxone) + else() + set(_detect_target_triplet_plat windows) + endif() + elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" + OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD" + )) + set(_detect_target_triplet_plat freebsd) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Android" + OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Android" + )) + set(_detect_target_triplet_plat android) + endif() + + if(EMSCRIPTEN) + set(_detect_target_triplet_arch wasm32) + set(_detect_target_triplet_plat emscripten) + endif() + + if(NOT _detect_target_triplet_arch STREQUAL "" + AND NOT _detect_target_triplet_plat STREQUAL "") + set(VCPKG_DETECT_TRIPLET_ARCH + ${_detect_target_triplet_arch} + PARENT_SCOPE) + set(VCPKG_DETECT_TRIPLET_PLAT + ${_detect_target_triplet_plat} + PARENT_SCOPE) + set(VCPKG_HOST_TRIPLET + "${_detect_target_triplet_arch}-${_detect_target_triplet_plat}" + PARENT_SCOPE) + endif() +endfunction() + +# load the triplet by VCPKG_TARGET_TRIPLET +macro(_vcpkg_load_triplet) + if(NOT DEFINED VCPKG_TARGET_TRIPLET) + message( + STATUS + "VCPKG_TARGET_TRIPLET is not defined, detecting triplet from the system" + ) + + _vcpkg_detect_host_triplet() + + if(DEFINED VCPKG_HOST_TRIPLET) + set(VCPKG_TARGET_TRIPLET "${VCPKG_HOST_TRIPLET}") + message(STATUS "Detected triplet: ${VCPKG_TARGET_TRIPLET}") + else() + message( + FATAL_ERROR + "Unable to determine target triplet, please set it manually.") + endif() + endif() + + # Load triplet variables from VCPKG_TARGET_TRIPLET triplet file + + set(_triplet "triplets/${VCPKG_TARGET_TRIPLET}.cmake") + set(_community_triplet "triplets/community/${VCPKG_TARGET_TRIPLET}.cmake") + + if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${_triplet}") + include("${CMAKE_CURRENT_LIST_DIR}/${_triplet}") + elseif(EXISTS "${_VCPKG_ROOT}/${_triplet}") + include("${_VCPKG_ROOT}/${_triplet}") + elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${_community_triplet}") + include("${CMAKE_CURRENT_LIST_DIR}/${_community_triplet}") + elseif(EXISTS "${_VCPKG_ROOT}/${_community_triplet}") + include("${_VCPKG_ROOT}/${_community_triplet}") + else() + message( + FATAL_ERROR + "Triplet ${VCPKG_TARGET_TRIPLET} not found at ${CMAKE_CURRENT_LIST_DIR}/${_triplet} or ${_VCPKG_ROOT}/${_triplet}" + ) + endif() + unset(_triplet) + unset(_community_triplet) +endmacro(_vcpkg_load_triplet) diff --git a/cmake/vcpkg/bootstrap/vcpkg_skip_install_on_reconfigure.cmake b/cmake/vcpkg/bootstrap/vcpkg_skip_install_on_reconfigure.cmake new file mode 100644 index 0000000..a6c51a5 --- /dev/null +++ b/cmake/vcpkg/bootstrap/vcpkg_skip_install_on_reconfigure.cmake @@ -0,0 +1,47 @@ +# SPDX-FileCopyrightText: Copyright 2024 Mikhail Svetkin +# SPDX-License-Identifier: MIT + +function(_vcpkg_update_manifest_hash) + set(_VCPKG_MANIFEST_HASH + "$CACHE{__VCPKG_MANIFEST_HASH}" + CACHE INTERNAL "Hash of vcpkg manifest file") +endfunction() + +# disable vcpkg manifest install step on cmake reconfigure if vcpkg.json has not +# changed. +function(_vcpkg_skip_install_on_reconfigure) + if(DEFINED CACHE{VCPKG_MANIFEST_DIR} AND NOT $CACHE{VCPKG_MANIFEST_DIR} + STREQUAL "") + set(vcpkg_manifest_file "$CACHE{VCPKG_MANIFEST_DIR}/vcpkg.json") + else() + set(vcpkg_manifest_file "${CMAKE_SOURCE_DIR}/vcpkg.json") + endif() + + file(SHA512 "${vcpkg_manifest_file}" vcpkg_manifest_hash) + + if(DEFINED CACHE{_VCPKG_MANIFEST_HASH} AND _VCPKG_MANIFEST_HASH STREQUAL + vcpkg_manifest_hash) + set(VCPKG_MANIFEST_INSTALL + OFF + CACHE INTERNAL "") + set_property( + DIRECTORY + APPEND + PROPERTY CMAKE_CONFIGURE_DEPENDS "${vcpkg_manifest_file}") + else() + set(VCPKG_MANIFEST_INSTALL + ON + CACHE INTERNAL "") + endif() + + # I was not able to propagate vcpkg_manifest_hash via defer call, so + # workaround it with another cache variable. + set(__VCPKG_MANIFEST_HASH + "${vcpkg_manifest_hash}" + CACHE INTERNAL "") + + # set actual hash only when vcpkg install command succeed. The only way you to + # detect to assume that configuration step succeed. + cmake_language(DEFER DIRECTORY ${CMAKE_SOURCE_DIR} CALL + _vcpkg_update_manifest_hash) +endfunction() diff --git a/cmake/vcpkg/ports/README.md b/cmake/vcpkg/ports/README.md new file mode 100644 index 0000000..a129778 --- /dev/null +++ b/cmake/vcpkg/ports/README.md @@ -0,0 +1,3 @@ +# Customize vcpkg ports + +This folder applies to the custom vcpkg ports. These ports are loaded automatically which can be patched from the `ports` folder of the vcpkg repository or created from scratch. diff --git a/cmake/vcpkg/ports/python3/portfile.cmake b/cmake/vcpkg/ports/python3/portfile.cmake new file mode 100644 index 0000000..e7d1e92 --- /dev/null +++ b/cmake/vcpkg/ports/python3/portfile.cmake @@ -0,0 +1,3 @@ +message(STATUS "Using fake python3 to force system libraries.") + +set(VCPKG_POLICY_EMPTY_PACKAGE enabled) diff --git a/cmake/vcpkg/ports/python3/vcpkg.json b/cmake/vcpkg/ports/python3/vcpkg.json new file mode 100644 index 0000000..e7749c5 --- /dev/null +++ b/cmake/vcpkg/ports/python3/vcpkg.json @@ -0,0 +1,7 @@ +{ + "name": "python3", + "version": "3.11.8", + "description": "The Python programming language", + "homepage": "https://github.com/python/cpython", + "license": "Python-2.0" +} diff --git a/cmake/vcpkg/scripts/build_type.cmake b/cmake/vcpkg/scripts/build_type.cmake new file mode 100644 index 0000000..a49dad6 --- /dev/null +++ b/cmake/vcpkg/scripts/build_type.cmake @@ -0,0 +1,26 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2024 msclock + +This module initializes the build type. +]] + +if(NOT CMAKE_BUILD_TYPE) + message(STATUS "Setting CMAKE_BUILD_TYPE to 'Debug' as none was specified.") +endif() +set(CMAKE_BUILD_TYPE + Debug + CACHE STRING "Choose the type of build.") + +# Set the possible values of build type for cmake-gui, ccmake +get_property(_builtin_build_types GLOBAL PROPERTY CMAKE_BUILD_TYPE) + +foreach(_type "Debug" "Release" "MinSizeRel" "RelWithDebInfo") + if(NOT _type IN_LIST _builtin_build_types) + list(APPEND _builtin_build_types ${_type}) + endif() +endforeach() + +set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${_builtin_build_types}) + +unset(_builtin_build_types) diff --git a/cmake/vcpkg/scripts/toolchains/README.md b/cmake/vcpkg/scripts/toolchains/README.md new file mode 100644 index 0000000..2b60435 --- /dev/null +++ b/cmake/vcpkg/scripts/toolchains/README.md @@ -0,0 +1,3 @@ +# Customize vcpkg toolchains + +This folder applies to the custom vcpkg toolchains. These toolchains can be loaded automatically which can be copied from the `scripts/toolchains` folder of the vcpkg repository or created from scratch. diff --git a/cmake/vcpkg/triplets/README.md b/cmake/vcpkg/triplets/README.md new file mode 100644 index 0000000..74db63a --- /dev/null +++ b/cmake/vcpkg/triplets/README.md @@ -0,0 +1,22 @@ +# Customize vcpkg triplets + +This folder applies to the custom vcpkg triplets. These triplets are loaded automatically which can be copied from the `triplets` folder of the vcpkg repository or created from scratch. + +For example, to create a custom triplet for Linux x64 with dynamic library linkage: + +```bash +# Copy the triplet from the vcpkg repository +cp /path/to/vcpkg/repository/triplets/x64-linux.cmake ./x64-linux.cmake +# Edit the triplet to change the library linkage to dynamic +sed -i ./x64-linux.cmake -e's/set(VCPKG_LIBRARY_LINKAGE static)/set(VCPKG_LIBRARY_LINKAGE dynamic)/g' +# Show the contents of the triplet +cat ./x64-linux.cmake +# set(VCPKG_TARGET_ARCHITECTURE x64) +# set(VCPKG_CRT_LINKAGE dynamic) +# set(VCPKG_LIBRARY_LINKAGE dynamic) + +# set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +# Use it when configure the project +cmake --preset=default -DVCPKG_TARGET_TRIPLET=x64-linux +``` diff --git a/cmake/vcpkg/triplets/arm64-osx.cmake b/cmake/vcpkg/triplets/arm64-osx.cmake new file mode 100644 index 0000000..418890c --- /dev/null +++ b/cmake/vcpkg/triplets/arm64-osx.cmake @@ -0,0 +1,11 @@ +set(VCPKG_TARGET_ARCHITECTURE arm64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Darwin) +set(VCPKG_OSX_ARCHITECTURES arm64) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/arm64-linux.cmake b/cmake/vcpkg/triplets/community/arm64-linux.cmake new file mode 100644 index 0000000..7511873 --- /dev/null +++ b/cmake/vcpkg/triplets/community/arm64-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE arm64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/mips64-linux.cmake b/cmake/vcpkg/triplets/community/mips64-linux.cmake new file mode 100644 index 0000000..e007033 --- /dev/null +++ b/cmake/vcpkg/triplets/community/mips64-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE mips64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/ppc64le-linux.cmake b/cmake/vcpkg/triplets/community/ppc64le-linux.cmake new file mode 100644 index 0000000..1b12e04 --- /dev/null +++ b/cmake/vcpkg/triplets/community/ppc64le-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE ppc64le) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/riscv64-linux.cmake b/cmake/vcpkg/triplets/community/riscv64-linux.cmake new file mode 100644 index 0000000..b8f24ba --- /dev/null +++ b/cmake/vcpkg/triplets/community/riscv64-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE riscv64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/s390x-linux.cmake b/cmake/vcpkg/triplets/community/s390x-linux.cmake new file mode 100644 index 0000000..cd2ad13 --- /dev/null +++ b/cmake/vcpkg/triplets/community/s390x-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE s390x) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/community/x86-linux.cmake b/cmake/vcpkg/triplets/community/x86-linux.cmake new file mode 100644 index 0000000..92842b7 --- /dev/null +++ b/cmake/vcpkg/triplets/community/x86-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE x86) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/x64-linux.cmake b/cmake/vcpkg/triplets/x64-linux.cmake new file mode 100644 index 0000000..6b3089a --- /dev/null +++ b/cmake/vcpkg/triplets/x64-linux.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE x64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/triplets/x64-osx.cmake b/cmake/vcpkg/triplets/x64-osx.cmake new file mode 100644 index 0000000..8a4d932 --- /dev/null +++ b/cmake/vcpkg/triplets/x64-osx.cmake @@ -0,0 +1,11 @@ +set(VCPKG_TARGET_ARCHITECTURE x64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) + +set(VCPKG_CMAKE_SYSTEM_NAME Darwin) +set(VCPKG_OSX_ARCHITECTURES x86_64) + +if(PORT MATCHES "7zip") + message(STATUS "Set to link 7zip dynamically") + set(VCPKG_LIBRARY_LINKAGE dynamic) +endif() diff --git a/cmake/vcpkg/vcpkg.toolchain.cmake b/cmake/vcpkg/vcpkg.toolchain.cmake new file mode 100644 index 0000000..19910c8 --- /dev/null +++ b/cmake/vcpkg/vcpkg.toolchain.cmake @@ -0,0 +1,31 @@ +#[[ +SPDX-License-Identifier: MIT +SPDX-FileCopyrightText: Copyright 2023 Mikhail Svetkin +SPDX-FileCopyrightText: Copyright 2024 msclock +]] + +include_guard(GLOBAL) + +cmake_minimum_required(VERSION 3.25) + +get_property(IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE) + +if(IN_TRY_COMPILE) + return() +endif() + +unset(IN_TRY_COMPILE) + +# Load the common settings +include(${CMAKE_CURRENT_LIST_DIR}/scripts/build_type.cmake) + +include(${CMAKE_CURRENT_LIST_DIR}/bootstrap/vcpkg-config.cmake) + +set(VCPKG_VERBOSE + ON + CACHE BOOL "Vcpkg VCPKG_VERBOSE") + +file(READ ${CMAKE_SOURCE_DIR}/vcpkg.json _vcpkg_json) +string(JSON _builtin_baseline GET ${_vcpkg_json} builtin-baseline) +vcpkg_configure(CACHE_DIR_NAME pybit7z REPO + https://github.com/microsoft/vcpkg.git REF ${_builtin_baseline}) diff --git a/docs/api/index.md b/docs/api/index.md new file mode 100644 index 0000000..860bf04 --- /dev/null +++ b/docs/api/index.md @@ -0,0 +1,17 @@ +# pybit7z package + +```{eval-rst} +.. automodule:: pybit7z + :members: + :undoc-members: + :show-inheritance: +``` + +## Submodules + +```{eval-rst} +.. automodule:: pybit7z._core + :members: + :undoc-members: + :show-inheritance: +``` diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..308dcd4 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,121 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) +from __future__ import annotations + +from pathlib import Path + +ROOT = Path(__file__).parent.parent.resolve() + +# -- Project information ----------------------------------------------------- + +project = "pybit7z" +copyright = "2024 l.feng" +author = "l.feng" + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + "myst_parser", + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.viewcode", + "sphinx.ext.mathjax", + "sphinx.ext.napoleon", + "sphinx_autodoc_typehints", + "sphinx_copybutton", +] +source_suffix = { + ".rst": "restructuredtext", + ".md": "markdown", +} +# Add any paths that contain templates here, relative to this directory. +templates_path = ["_templates"] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = [ + "_build", + "Thumbs.db", + ".DS_Store", + ".env", + ".venv", +] + +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-the-linkcheck-builder +linkcheck_ignore = [ + "https://github.com/msclock/pybit7z", + "https://msclock.github.io/pybit7z", +] + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of buhltin themes. +# +# default theme +# html_theme = 'alabaster' +# +# use furo +html_theme = "furo" + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +# html_static_path = ["_static"] + +html_favicon = "https://img.shields.io/badge/P-blue" + +html_css_files = [ + "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/fontawesome.min.css", + "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/solid.min.css", + "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/brands.min.css", +] + +html_theme_options = { + "footer_icons": [ + { + "name": "GitHub", + "url": "https://github.com/msclock/pybit7z", + "html": "", + "class": "fa-brands fa-solid fa-github fa-lg", + }, + ], +} + +# -- Extension configuration ------------------------------------------------- +myst_enable_extensions = [ + "colon_fence", + "substitution", + "deflist", +] + +# https://myst-parser.readthedocs.io/en/latest/syntax/optional.html#auto-generated-header-anchors +myst_heading_anchors = 3 + +intersphinx_mapping = { + "python": ("https://docs.python.org/3", None), +} + +nitpick_ignore = [ + ("py:class", "_io.StringIO"), + ("py:class", "_io.BytesIO"), +] + +always_document_param_types = True diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..d382988 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,34 @@ +# Welcome to pybit7z's documentation + + + +```{include} ../README.md +:start-after: +``` + +## Content + +```{toctree} +:maxdepth: 2 +:titlesonly: +:caption: Guide +:glob: + +Overview +``` + + + +```{toctree} +:maxdepth: 1 +:titlesonly: +:caption: API docs + +api/index +``` + +## Indices and tables + +- {ref}`genindex` +- {ref}`modindex` +- {ref}`search` diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..e711824 --- /dev/null +++ b/noxfile.py @@ -0,0 +1,92 @@ +from __future__ import annotations + +import argparse +import shutil +from pathlib import Path + +import nox + +DIR = Path(__file__).parent.resolve() + +nox.options.sessions = ["lint", "pylint", "tests"] + + +@nox.session +def lint(session: nox.Session) -> None: + """ + Run the linter. + """ + session.install("pre-commit") + session.run( + "pre-commit", "run", "--all-files", "--show-diff-on-failure", *session.posargs + ) + + +@nox.session +def pylint(session: nox.Session) -> None: + """ + Run PyLint. + """ + # This needs to be installed into the package environment, and is slower + # than a pre-commit check + session.install(".", "pylint") + session.run("pylint", "pybit7z", *session.posargs) + + +@nox.session +def tests(session: nox.Session) -> None: + """ + Run the unit and regular tests. + """ + session.install(".[test]") + session.run("pytest", *session.posargs) + + +@nox.session(reuse_venv=True) +def docs(session: nox.Session) -> None: + """ + Build the docs. Pass "-- --help" to show helps. + """ + parser = argparse.ArgumentParser() + parser.add_argument("--serve", action="store_true", help="Serve after building") + parser.add_argument( + "--check", action="store_true", help="Checks the docs with warnings as errors." + ) + parser.add_argument("--linkcheck", action="store_true", help="Checks the links.") + args, posargs = parser.parse_known_args(session.posargs) + + extra_installs = ["sphinx-autobuild"] if args.serve else [] + session.install("-e.[docs]", *extra_installs) + + shared_args = [ + "-T", # full tracebacks + "-c", + "docs", + "docs", + "docs/_build/html", + *posargs, + ] + + if args.check: + shared_args.append("-n") # nitpicky mode + + if args.serve: + session.run("sphinx-autobuild", *shared_args) + elif args.linkcheck: + session.run("sphinx-build", "-b", "linkcheck", *shared_args) + else: + session.run("sphinx-build", "--keep-going", *shared_args) + + +@nox.session +def build(session: nox.Session) -> None: + """ + Build an SDist and wheel. + """ + + build_path = DIR.joinpath("build") + if build_path.exists(): + shutil.rmtree(build_path) + + session.install("build") + session.run("python", "-m", "build") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d69ce07 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,171 @@ +[build-system] +requires = ["scikit-build-core"] +build-backend = "scikit_build_core.build" + +[project] +name = "pybit7z" +description = "A wrapper based on bit7z." +readme = "README.md" +license = { text = "Apache-2.0" } +authors = [{ email = "msclock@126.com", name = "l.feng" }] +requires-python = ">=3.8" +classifiers = [ + "Intended Audience :: Science/Research", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Scientific/Engineering", + "Typing :: Typed", +] +dynamic = ["version"] +dependencies = [ + "typing-extensions>=3.10.0; python_version<'3.9'", + "importlib_metadata>=1.4", +] + +[project.optional-dependencies] +test = ["pytest >=6", "pytest-cov >=3"] +dev = ["pytest >=6", "pytest-cov >=3"] +docs = [ + "sphinx>=7.0", + "myst_parser>=0.13", + "sphinx_copybutton", + "sphinx_autodoc_typehints", + "furo>=2023.08.17", +] + +[project.urls] +Homepage = "https://github.com/msclock/pybit7z" +"Bug Tracker" = "https://github.com/msclock/pybit7z/issues" +Discussions = "https://github.com/msclock/pybit7z/discussions" +Changelog = "https://github.com/msclock/pybit7z/releases" + +[tool.scikit-build] +minimum-version = "0.9" +build-dir = "build/{wheel_tag}" +cmake.verbose = false +logging.level = "DEBUG" +metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" +sdist.include = ["src/pybit7z/_version.py"] +cmake.args = [ + "-DBUILD_TESTING=OFF", + "-DCMAKE_BUILD_TYPE=Release", + "-DVCPKG_INSTALL_REPORT_FAILURE=ON", + "-DVCPKG_INSTALL_OPTIONS=--allow-unsupported", + "-DCMAKE_TOOLCHAIN_FILE=cmake/vcpkg/vcpkg.toolchain.cmake", + "-DVCPKG_OVERLAY_TRIPLETS=cmake/vcpkg/triplets", + "-DVCPKG_OVERLAY_PORTS=cmake/vcpkg/ports", +] + +[tool.setuptools_scm] +write_to = "src/pybit7z/_version.py" + +[tool.cibuildwheel] +build-verbosity = 1 +test-command = "pytest {project}/tests" +test-extras = ["test"] +test-skip = ["*-win_arm64", "*-macosx_universal2:arm64"] + +[tool.cibuildwheel.linux] +before-build = [ + 'yum install zip -y || apk add zip || true', + 'apt update && apt install zip pkg-config -y || true', + 'pip install cmake ninja', + 'cmake --version', + 'git --version', + 'ninja --version', +] + +[[tool.cibuildwheel.overrides]] +select = "*linux_{aarch64,armv7l,ppc64le,s390x}" +environment = { VCPKG_FORCE_SYSTEM_BINARIES = "1" } + +[tool.pytest.ini_options] +minversion = "6.0" +addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] +xfail_strict = true +filterwarnings = ["error"] +log_cli_level = "INFO" +testpaths = ["tests"] + +[tool.coverage] +run.source = ["pybit7z"] +report.exclude_also = ['\.\.\.', 'if typing.TYPE_CHECKING:'] + +[tool.mypy] +files = ["src", "tests"] +python_version = "3.8" +warn_unused_configs = true +strict = true +enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] +warn_unreachable = true +disallow_untyped_defs = false +disallow_incomplete_defs = false + +[[tool.mypy.overrides]] +module = "pybit7z.*" +disallow_untyped_defs = true +disallow_incomplete_defs = true + +[tool.ruff] +src = ["src"] + +[tool.ruff.lint] +extend-select = [ + "B", # flake8-bugbear + "I", # isort + "ARG", # flake8-unused-arguments + "C4", # flake8-comprehensions + "EM", # flake8-errmsg + "ICN", # flake8-import-conventions + "G", # flake8-logging-format + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + "PT", # flake8-pytest-style + "PTH", # flake8-use-pathlib + "RET", # flake8-return + "RUF", # Ruff-specific + "SIM", # flake8-simplify + "T20", # flake8-print + "UP", # pyupgrade + "YTT", # flake8-2020 + "EXE", # flake8-executable + "NPY", # NumPy specific rules + "PD", # pandas-vet +] +ignore = [ + "PLR09", # Too many <...> + "PLR2004", # Magic value used in comparison + "ISC001", # Conflicts with formatter +] +isort.required-imports = ["from __future__ import annotations"] +# Uncomment if using a _compat.typing backport +# typing-modules = ["pybit7z._compat.typing"] + +[tool.ruff.lint.per-file-ignores] +"tests/**" = ["T20"] +"noxfile.py" = ["T20"] + +[tool.pylint] +py-version = "3.8" +ignore-paths = [".*/_version.py"] +extension-pkg-allow-list = ["pybit7z._core"] +reports.output-format = "colorized" +similarities.ignore-imports = "yes" +messages_control.disable = [ + "design", + "fixme", + "line-too-long", + "missing-module-docstring", + "wrong-import-position", +] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..3b11226 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,12 @@ +file( + GLOB _sub_dirs + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/*) + +foreach(_sub_dir ${_sub_dirs}) + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_sub_dir}/CMakeLists.txt") + add_subdirectory(${_sub_dir}) + endif() +endforeach() + +unset(_sub_dirs) diff --git a/src/_core/CMakeLists.txt b/src/_core/CMakeLists.txt new file mode 100644 index 0000000..72f1645 --- /dev/null +++ b/src/_core/CMakeLists.txt @@ -0,0 +1,65 @@ +set(PYBIND11_FINDPYTHON ON) + +find_package(7zip CONFIG REQUIRED) +find_package(pybind11 CONFIG REQUIRED) +find_package(unofficial-bit7z CONFIG REQUIRED) + +# The internal target will not be installed and only act as requirements for +# main and tests internally +set(target_name_internal _core_internal) +set(target_name _core) + +file(GLOB_RECURSE internal_srcs "src/*.cpp") +file(GLOB_RECURSE internal_hdrs "include/*.hpp") + +list(FILTER internal_srcs EXCLUDE REGEX "pybind.cpp") + +add_library(${target_name_internal} OBJECT) +warn_target(${target_name_internal}) +harden_target(${target_name_internal}) +sanitize_target(${target_name_internal}) + +generate_git_header(VERSION_NAMESPACE_PREFIX _core) + +target_sources(${target_name_internal} PRIVATE ${internal_srcs} + ${internal_hdrs}) + +target_include_interface_directories( + ${target_name_internal} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_BINARY_DIR}/git_version) + +target_link_libraries(${target_name_internal} PUBLIC 7zip::7zip + unofficial::bit7z::bit7z64) + +# The main target only contains main logits. Here it is recommended to put the +# parts that need to be verified and tested in the target target_name_internal +set(target_name _core) + +pybind11_add_module(${target_name} MODULE ${internal_srcs} ${internal_hdrs} + src/pybind.cpp) +warn_target(${target_name}) +harden_target(${target_name}) +sanitize_target(${target_name}) + +target_link_libraries(${target_name} PRIVATE ${target_name_internal}) + +add_custom_command( + TARGET ${target_name} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ + $ + COMMENT "Sync 7zip runtime library to build path.") + +install(IMPORTED_RUNTIME_ARTIFACTS 7zip::7zip + RUNTIME_DEPENDENCY_SET plugins_runtime + DESTINATION ${SKBUILD_PROJECT_NAME} + COMPONENT Runtime) + +install(TARGETS ${target_name} DESTINATION ${SKBUILD_PROJECT_NAME}) + +install_dependency(TARGETS ${target_name} DEPENDS_DESTINATION + ${SKBUILD_PROJECT_NAME}) + +if(BUILD_TESTING) + add_test_subdirectory(tests) +endif() diff --git a/src/_core/include/_core.hpp b/src/_core/include/_core.hpp new file mode 100644 index 0000000..22f85d8 --- /dev/null +++ b/src/_core/include/_core.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "_version.hpp" +#include "pybit7z.hpp" diff --git a/src/_core/include/pybit7z.hpp b/src/_core/include/pybit7z.hpp new file mode 100644 index 0000000..82dc938 --- /dev/null +++ b/src/_core/include/pybit7z.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace _core { +#ifdef WIN32 +#if defined(_MSC_VER) +constexpr auto default_lib7zip = "7zip.dll"; +#else +constexpr auto default_lib7zip = "lib7zip.dll"; +#endif +#else +constexpr auto default_lib7zip = "lib7zip.so"; +#endif +} // namespace _core diff --git a/src/_core/src/pybind.cpp b/src/_core/src/pybind.cpp new file mode 100644 index 0000000..9742523 --- /dev/null +++ b/src/_core/src/pybind.cpp @@ -0,0 +1,19 @@ +#include + +#include "_core.hpp" + +namespace py = pybind11; + +PYBIND11_MODULE(_core, m) { + m.doc() = R"pbdoc( + Pybind11 _core plugin + ----------------------- + .. currentmodule:: _core + )pbdoc"; + + m.def("version", []() { return _core::ProjectVersion(); }, R"pbdoc( + The _core plugin version. + )pbdoc"); + + m.attr("default_lib7zip") = _core::default_lib7zip; +} diff --git a/src/_core/src/pybit7z.cpp b/src/_core/src/pybit7z.cpp new file mode 100644 index 0000000..d25a863 --- /dev/null +++ b/src/_core/src/pybit7z.cpp @@ -0,0 +1,2 @@ +#include "pybit7z.hpp" + diff --git a/src/_core/tests/CMakeLists.txt b/src/_core/tests/CMakeLists.txt new file mode 100644 index 0000000..412a15f --- /dev/null +++ b/src/_core/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +find_package(GTest CONFIG REQUIRED) + +include(GoogleTest) + +file(GLOB files "*.cpp") + +foreach(_file ${files}) + get_filename_component(file_basename ${_file} NAME_WE) + add_executable(${file_basename} ${_file}) + target_link_libraries(${file_basename} PRIVATE GTest::gtest_main + _core_internal) + target_compile_definitions(${file_basename} + PRIVATE TESTS_DIR="${CMAKE_CURRENT_SOURCE_DIR}") + add_custom_command( + TARGET ${file_basename} + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ + $ + COMMENT "Sync 7zip runtime library to build path.") + + warn_target(${file_basename}) + harden_target(${file_basename}) + sanitize_target(${file_basename}) + gtest_discover_tests(${file_basename}) +endforeach() diff --git a/src/_core/tests/test_core.cpp b/src/_core/tests/test_core.cpp new file mode 100644 index 0000000..efa8322 --- /dev/null +++ b/src/_core/tests/test_core.cpp @@ -0,0 +1,60 @@ +#include "gtest/gtest.h" + +#include "_core.hpp" +#include "utils.hpp" + +TEST(_core, version) { + auto version = _core::ProjectVersion(); + EXPECT_TRUE(!version.empty()); +} + +using _core_pybit7z_test = test::utils::rc_dir_test; + +TEST_F(_core_pybit7z_test, compress) { + using namespace bit7z; + + Bit7zLibrary lib{_core::default_lib7zip}; + BitFileCompressor compressor{lib, BitFormat::Zip}; + + std::vector files = {this->tests_dir.string() + "/test_core.cpp"}; + + // Creating a simple zip archive + compressor.compress(files, this->system_test_tmp_dir_.string() + "/output_archive.zip"); + + // Creating a zip archive with a custom directory structure + std::map files_map = { + {this->tests_dir.string() + "/test_core.cpp", "alias/path/test_core.cpp"}}; + compressor.compress(files_map, this->system_test_tmp_dir_.string() + "/custom_dir_archive.zip"); + + // Compressing a directory + compressor.compressDirectory(this->tests_dir.string() + "/.." + "/src", + this->system_test_tmp_dir_.string() + "/dir_archive.zip"); + + // Creating an encrypted zip archive of two files + compressor.setPassword("password"); + compressor.compressFiles(files, this->system_test_tmp_dir_.string() + "/protected_archive.zip"); + + // Updating an existing zip archive + compressor.setUpdateMode(UpdateMode::Append); + compressor.compressFiles(files, this->system_test_tmp_dir_.string() + "/existing_archive.zip"); + + // Compressing a single file into a buffer + std::vector buffer; + BitFileCompressor compressor2{lib, BitFormat::BZip2}; + compressor2.compressFile(files[0], buffer); +} + +TEST_F(_core_pybit7z_test, archive_writer) { + using namespace bit7z; + + Bit7zLibrary lib{_core::default_lib7zip}; + BitArchiveWriter archive{lib, BitFormat::SevenZip}; + + // Adding the items to be compressed (no compression is performed here) + archive.addFile(this->tests_dir.string() + "/test_core.cpp"); + archive.addDirectory(this->tests_dir.string() + "/.." + "/include"); + archive.addDirectory(this->tests_dir.string() + "/.." + "/src"); + + // Compressing the added items to the output archive + archive.compressTo(this->system_test_tmp_dir_.string() + "/output.7z"); +} diff --git a/src/_core/tests/test_data/.gitignore b/src/_core/tests/test_data/.gitignore new file mode 100644 index 0000000..a9a5aec --- /dev/null +++ b/src/_core/tests/test_data/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/src/_core/tests/utils.hpp b/src/_core/tests/utils.hpp new file mode 100644 index 0000000..0d3161e --- /dev/null +++ b/src/_core/tests/utils.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include + +#ifndef TESTS_DIR +#error "TESTS_DIR is not defined" +#endif + +#define TEST_DATA_DIR_NAME "test_data" +#define TEST_TEMP_DIR_NAME "tmp" + +namespace test { +namespace utils { +class rc_dir_test : public ::testing::Test { +protected: + std::filesystem::path tests_dir{TESTS_DIR}; + std::filesystem::path test_data_dir_; + std::filesystem::path system_test_tmp_dir_; + + void SetUp() override { + this->test_data_dir_ = this->tests_dir / TEST_DATA_DIR_NAME; + std::mt19937_64 gen(std::random_device{}()); + std::uniform_int_distribution<> dis(1000, 9999); + auto suffix_dir_name = std::to_string(dis(gen)) + std::to_string(dis(gen)); + this->system_test_tmp_dir_ = this->test_data_dir_ / TEST_TEMP_DIR_NAME / suffix_dir_name; + std::filesystem::create_directories(system_test_tmp_dir_); + } + + void TearDown() override { std::filesystem::remove_all(system_test_tmp_dir_); } +}; +} // namespace utils +} // namespace test diff --git a/src/pybit7z/__init__.py b/src/pybit7z/__init__.py new file mode 100644 index 0000000..d26b1fc --- /dev/null +++ b/src/pybit7z/__init__.py @@ -0,0 +1,11 @@ +""" +Copyright (c) 2024 l.feng. All rights reserved. + +pybit7z: A wrapper based on bit7z. +""" + +from __future__ import annotations + +from ._version import version as __version__ + +__all__ = ["__version__"] diff --git a/src/pybit7z/_core.pyi b/src/pybit7z/_core.pyi new file mode 100644 index 0000000..51fb416 --- /dev/null +++ b/src/pybit7z/_core.pyi @@ -0,0 +1,6 @@ +"""Pybind11 _core plugin interfaces""" + +from __future__ import annotations + +def version() -> str: + """_core plugin version.""" diff --git a/src/pybit7z/_version.pyi b/src/pybit7z/_version.pyi new file mode 100644 index 0000000..91744f9 --- /dev/null +++ b/src/pybit7z/_version.pyi @@ -0,0 +1,4 @@ +from __future__ import annotations + +version: str +version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str] diff --git a/src/pybit7z/py.typed b/src/pybit7z/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_compiled.py b/tests/test_compiled.py new file mode 100644 index 0000000..a5f5333 --- /dev/null +++ b/tests/test_compiled.py @@ -0,0 +1,7 @@ +from __future__ import annotations + +from pybit7z import _core + + +def test_core_version(): + assert _core.version() != "" diff --git a/tests/test_package.py b/tests/test_package.py new file mode 100644 index 0000000..659dacb --- /dev/null +++ b/tests/test_package.py @@ -0,0 +1,9 @@ +from __future__ import annotations + +from importlib_metadata import version + +import pybit7z as m + + +def test_version(): + assert version("pybit7z") == m.__version__ diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..f876901 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,72 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", + "name": "pybit7z", + "description": "A wrapper based on bit7z.", + "builtin-baseline": "20a72ce99b12dd0ebfea5d39f32681bd68b19d03", + "homepage": "https://github.com/msclock/pybit7z", + "dependencies": [ + "pybind11", + { + "name": "bit7z", + "features": [ + "regex-matching" + ] + }, + "7zip", + "cmake-modules", + "robotology-cmake-ycm" + ], + "overrides": [ + { + "name": "pybind11", + "version": "2.13.5" + }, + { + "name": "bit7z", + "version": "4.0.8" + }, + { + "name": "7zip", + "version-string": "24.08" + }, + { + "name": "cmake-modules", + "version": "1.6.11" + }, + { + "name": "robotology-cmake-ycm", + "version": "0.16.3" + } + ], + "features": { + "test": { + "description": "Dependencies for testing", + "dependencies": [ + { + "name": "gtest", + "version>=": "1.14.0" + } + ] + } + }, + "vcpkg-configuration": { + "registries": [ + { + "kind": "git", + "baseline": "09ae1472980f5e2132654900bb48061444b1cea5", + "repository": "https://github.com/msclock/cmake-registry", + "packages": [ + "cmake-modules", + "robotology-cmake-ycm", + "vcpkg-cmake-module" + ] + } + ], + "overlay-ports": [ + "cmake/vcpkg/ports" + ], + "overlay-triplets": [ + "cmake/vcpkg/triplets" + ] + } +}