Skip to content

How can I update a C header file with the version? #1348

Closed

Description

@garrethcain

Question

I am trying to update a C header file with the version. I have managed to get the file to update using build_command but this doesn't seem to get pushed along with the Changelog when PSR runs.

I tried several different ways to configure the version variable, but it never updated the Header file, but does update the pyproject/py file okay.
Using the build_command does update the variable but it doesn't push header back along with the TAG so the next build step, that runs when a tag is pushed, is always out of date.

Here is my header file:

#ifndef VERSION_H
#define VERSION_H

#define APP_VERSION "0.0.0"

#endif // VERSION_H

This is my build_command:

VERSION=$(uvx python-semantic-release version --print)
echo '#define APP_VERSION "$VERSION"' > src/version.h

And this is my version_variable, I tried many different formats;

src/version.h:#define APP_VERSION

Configuration

[project]
version = "1.1.0-rc.8"    <-- updates OK.

[tool.semantic_release]
assets = []
build_command_env = []
commit_message = "{version}\n\nAutomatically generated by python-semantic-release"
commit_parser = "conventional"
logging_use_named_masks = false
major_on_zero = true
allow_zero_version = false
no_git_verify = false
tag_format = "v{version}"
version_variable = ["src/version.h:#define APP_VERSION"]
version_toml = ["pyproject.toml:project.version"]
build_command = """
VERSION=$(uvx python-semantic-release version --print)
echo '#define APP_VERSION "$VERSION"' > src/version.h
git add src/version.h
"""

[tool.semantic_release.branches.master]
match = "(main|master)"
prerelease_token = "rc"
prerelease = false

[tool.semantic_release.branches.develop]
match = "develop"
prerelease = true
prerelease_token = "rc"

[tool.semantic_release.changelog]
exclude_commit_patterns = []
mode = "update"
insertion_flag = "<!-- version list -->"
template_dir = "templates"

[tool.semantic_release.changelog.default_templates]
changelog_file = "CHANGELOG.md"
output_format = "md"
mask_initial_release = true

[tool.semantic_release.changelog.environment]
block_start_string = "{%"
block_end_string = "%}"
variable_start_string = "{{"
variable_end_string = "}}"
comment_start_string = "{#"
comment_end_string = "#}"
trim_blocks = false
lstrip_blocks = false
newline_sequence = "\n"
keep_trailing_newline = false
extensions = []
autoescape = false

[tool.semantic_release.commit_author]
env = "GIT_COMMIT_AUTHOR"
default = "semantic-release <semantic-release>"

[tool.semantic_release.commit_parser_options]
minor_tags = ["feat"]
patch_tags = ["fix", "perf"]
other_allowed_tags = [
  "build",
  "chore",
  "ci",
  "docs",
  "style",
  "refactor",
  "test",
]
allowed_tags = [
  "feat",
  "fix",
  "perf",
  "build",
  "chore",
  "ci",
  "docs",
  "style",
  "refactor",
  "test",
]
default_bump_level = 0
parse_squash_commits = true
ignore_merge_commits = true

[tool.semantic_release.remote]
name = "origin"
type = "gitlab"
ignore_token_for_push = false
insecure = false

[tool.semantic_release.publish]
dist_glob_patterns = ["dist/*"]
upload_to_vcs_release = true

Additional context

Added above already.

Thanks for the help in advance.

Activity

  1. changed the title [-]How can I update a C header file with teh version?[/-] [+]How can I update a C header file with the version?[/+] on Nov 2, 2025
  2. added
    featureA new feature or a feature request
    confirmedPrevent from becoming stale
    and removed
    triagewaiting for initial maintainer review
    on Nov 2, 2025
  3. codejedi365 commented on Nov 2, 2025

    @codejedi365
    Contributor

    @garrethcain, after some testing, I realized that the current version_variables setting and corresponding regular expression that is created does not support the space separation variable definition that is found in a C macro. I will have to modify the PatternVersionDeclaration object to support a single space separation. Sorry for the inconvenience!

  4. garrethcain commented on Nov 2, 2025

    @garrethcain
    Author

    Thanks for the update and excellent support.

    Looking forward to a fix in a future release. 👍

  5. codejedi365 commented on Nov 10, 2025

    @codejedi365
    Contributor

    🎉 This issue has been resolved in v10.5.0 🎉

    You can find more information about this release on the GitHub Releases page.

    If your issue persists, please reply here and we will re-open the issue. If you receive a new error, please open a new issue and reference this issue there.

  6. garrethcain commented on Nov 10, 2025

    @garrethcain
    Author

    This seems to still be an issue;

    version_variable = ["src/version.h:#define APP_VERSION"]
    version_toml = ["pyproject.toml:project.version"]
    

    Header file is as in the post above, the file is never modified but the pyproject.toml is updated as always.

  7. codejedi365 commented on Nov 10, 2025

    @codejedi365
    Contributor

    @garrethcain, Oh forgot to tell you to remove "#define" and just use APP_VERSION.

    And depending if you want the version as the associated tag then add :tf for tag format

  8. garrethcain commented on Nov 10, 2025

    @garrethcain
    Author

    I tried that way too. After reading the PR that's how it looked it worked.

    version_variable = ["src/version.h:APP_VERSION"]

  9. codejedi365 commented on Nov 10, 2025

    @codejedi365
    Contributor

    Oh your setting is wrong. It's version_variables with an s.

  10. garrethcain commented on Nov 10, 2025

    @garrethcain
    Author

    That's pretty funny. It was never going to work right from the beginning.

    That fixed it. Thanks again!

  11. codejedi365 commented on Nov 10, 2025

    @codejedi365
    Contributor

    Looking to refactor the configuration in a major release so that errors like that are not quietly suppressed but instead throw an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedPrevent from becoming stalefeatureA new feature or a feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions