Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add minimal LibC module #347

Merged
merged 18 commits into from
Jan 9, 2025
Merged

Add minimal LibC module #347

merged 18 commits into from
Jan 9, 2025

Conversation

TravisCardwell
Copy link
Collaborator

The purpose of this PR is to merge a minimal LibC implementation so that we can start thinking about how to integrate it into hs-bindgen.

Here is the relevant commit message:

This commit includes the "easy" types. Once merged, we can start work on integrating using of the library while simultaneously filling in the details.

Notes:

  • The x86_64 architecture Arch module is put in place, though no architecture-specific types that require it are implemented yet. TODO comments marked "arch" need to be implemented in Arch modules.
  • As decided on in our last call, integral types with a specific bit width are implemented using corresponding Haskell types. This allows them to be implemented in general, not in Arch modules.
  • Documentation indicates that stdint.h should define types int_fast8_t, int_fast16_t, int_fast32_t, and int_fast64_t (as well as the unsigned versions), but Musl does not define int_fast16_t or int_fast32_t (or the unsigned versions). I went ahead and implemented them in the module anyway.
  • No types from stdatomic.h, stdbit.h, or stdckdint.h are implemented yet. These are not (yet) supported by Musl.
  • The types implemented use names that match those in base. I can rename them to use our default name mangling rules if we decide to do that instead. I did not implement any structures or enumerations yet; I will do so once we decide on naming.

Other changes included in this PR:

  • bootstrap/standard_headers.h is documented, with links to references.
  • Vendored Musl headers are now stored per architecture. Bash script musl-update.sh can be used to easily add architectures as well as update to new versions, without requiring any cross-compilation toolchains.
  • The Musl VERSION and COPYRIGHT files are now included.
  • Executable hs-bindgen top-level options are added:
    • --standard allows selection of the C standard.
    • --gnu enables GNU extensions.
    • --target allows specification of the target architecture (triple).
  • We now use c17 by default, instead of gnu17 (the Clang default).
  • The prelude command no longer appends to the log file; it instead overwrites any existing log file with a new one.
  • The prelude command now has an --include option for specifying the header directory to use. When Musl headers for the host architecture are vendored, it defaults using them. Otherwise, the user must specify a directory.

Copy link
Collaborator

@edsko edsko left a comment

Choose a reason for hiding this comment

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

Reviewed over meet. Feel free to merge after making the changes discussed.

* References to standards are added.
* Headers that are not included in musl are commented out and
  documented.
This is just done so that we have a place to put the Musl `VERSION` and
`COPYRIGHT` files.
Using `appendFile` caused the log file to accumulate logs over multiple
executions.  This commit instead opens the log file in `Main`,
overwriting any existing log file.  Since we already use a tracer for
logging, logging to this file is also done using a tracer for
consistency.
The Clang default is `gnu17`, which is not what we want.  This commit
implements a new `--standard` CLI option that defaults to `c17`.  To
avoid confusion, `-std` options are prohibited from being specified in a
`--clang-option`.
This is done so that we can work with the headers for different
architectures.
The headers can be installed *without* any compilation if configuration
is done manually, not using the `configure` script since it checks for
cross-compilation toolchains.

The script no longer has to run on an `x86_64` architecture.  Since
toolchains are no longer needed, this enables us to easily vendor
headers for more architectures.
The type is still `String`, since the target triple is system-dependent
in that it depends on the name of the toolchains installed.  If/when we
need to use this value for other purposes, we will likely need to parse
it to a different type.
Setting an include directory is required for the command to work.  This
option sets a default when there are vendored Musl headers for the host
architecture.  When there are not vendored Musl headers for the host
architecture, the user must specify the option.

Option `-nostdinc` is now always set for this command.  Oddly, this
option is not displayed in the `--help` output on my system, but there
is no error when using it, and I see it in the current documentation
online.

The include directory is passed to Clang via `-isystem`.
This commit includes the "easy" types.  Once merged, we can start work
on integrating using of the library while simultaneously filling in the
details.

Notes:

* The `x86_64` architecture `Arch` module is put in place, though no
  architecture-specific types that require it are implemented yet.  TODO
  comments marked "`arch`" need to be implemented in `Arch` modules.
* As decided on in our last call, integral types with a specific bit
  width are implemented using corresponding Haskell types.  This allows
  them to be implemented in general, not in `Arch` modules.
* Documentation indicates that `stdint.h` should define types
  `int_fast8_t`, `int_fast16_t`, `int_fast32_t`, and `int_fast64_t` (as
  well as the unsigned versions), but Musl does *not* define
  `int_fast16_t` or `int_fast32_t` (or the unsigned versions).  I went
  ahead and implemented them in the module anyway.
* No types from `stdatomic.h`, `stdbit.h`, or `stdckdint.h` are
  implemented yet.  These are not (yet) supported by Musl.
* The types implemented use names that match those in `base`.  I can
  rename them to use our default name mangling rules if we decide to do
  that instead.  I did not implement any structures or enumerations yet;
  I will do so once we decide on naming.
@TravisCardwell
Copy link
Collaborator Author

Changes:

We will do doubt make further changes as we refine the design, but I am merging this as an initial implementation.

@TravisCardwell TravisCardwell merged commit 1611562 into main Jan 9, 2025
10 checks passed
@TravisCardwell TravisCardwell deleted the c-types-prelude branch January 9, 2025 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants