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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ unversioned
cabal.project.local
.vscode/
*.dll
macros-recognized.log
62 changes: 42 additions & 20 deletions hs-bindgen-libclang/src/HsBindgen/Clang/Args.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE LambdaCase #-}

module HsBindgen.Clang.Args (
ClangArgs(..)
, CStandard(..)
Expand All @@ -15,6 +17,9 @@ import HsBindgen.Clang.Version

-- | @libclang@ command line arguments
--
-- The default standard when one is not specified depends on the Clang version
-- and has GNU extensions enabled.
--
-- TODO: <https://github.com/well-typed/hs-bindgen/issues/83> (also #10 and #71).
-- We should support more of the command line arguments of @clang@.
data ClangArgs = ClangArgs {
Expand All @@ -24,6 +29,9 @@ data ClangArgs = ClangArgs {
-- | C standard
, clangCStandard :: Maybe CStandard

-- | Enable GNU extensions when 'True'
, clangEnableGnu :: Bool

-- | Other arguments
--
-- See https://clang.llvm.org/docs/ClangCommandLineReference.html
Expand All @@ -43,17 +51,18 @@ data ClangArgs = ClangArgs {
-- We don't currently support @C2y@ because it requires @clang-19@ or later and
-- we have no reliable way to test for that (see 'ClangVersion').
data CStandard =
C23
| C17
| C11
C89
| C99
| C89
deriving stock (Show, Eq)
| C11
| C17
| C23
deriving stock (Bounded, Enum, Eq, Ord, Show)

defaultClangArgs :: ClangArgs
defaultClangArgs = ClangArgs {
clangTarget = Nothing
, clangCStandard = Nothing
, clangEnableGnu = False
, clangOtherArgs = []
}

Expand All @@ -66,28 +75,41 @@ fromClangArgs args = aux [
ifGiven clangTarget $ \target ->
return ["-target", target]

, ifGiven clangCStandard $ \cStandard ->
case cStandard of
C23 -> -- We can use @-std=c23@ in @clang-18@ or later, but we have no
-- reliable way of testing for that.
if clangVersion >= Clang9_or_10
then return ["-std=c2x"]
else throwError "C23 requires clang-9 or later"
C17 -> if clangVersion >= Clang6
then return ["-std=c17"]
else throwError "C17 requires clang-6 or later"
C11 -> if clangVersion == ClangOlderThan3_2
then return ["-std=c1x"]
else return ["-std=c11"]
C99 -> return ["-std=c99"]
C89 -> return ["-std=c89"]
, ifGiven clangCStandard $ \case
C89
| clangEnableGnu -> return ["-std=gnu89"]
| otherwise -> return ["-std=c89"]
C99
| clangEnableGnu -> return ["-std=gnu99"]
| otherwise -> return ["-std=c99"]
C11
| clangEnableGnu -> return $
if clangVersion == ClangOlderThan3_2
then ["-std=gnu1x"]
else ["-std=gnu11"]
| otherwise -> return $
if clangVersion == ClangOlderThan3_2
then ["-std=c1x"]
else ["-std=c11"]
C17
| clangVersion < Clang6 -> throwError "C17 requires clang-6 or later"
| clangEnableGnu -> return ["-std=gnu17"]
| otherwise -> return ["-std=c17"]
-- We can use @-std=c23@ in @clang-18@ or later, but we have no reliable
-- way of testing for that.
C23
| clangVersion < Clang9_or_10 ->
throwError "C23 requires clang-9 or later"
| clangEnableGnu -> return ["-std=gnu2x"]
| otherwise -> return ["-std=c2x"]

, return clangOtherArgs
]
where
ClangArgs{
clangTarget
, clangCStandard
, clangEnableGnu
, clangOtherArgs
} = args

Expand Down
11 changes: 10 additions & 1 deletion hs-bindgen-patterns/hs-bindgen-patterns.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@ library
import:
lang
exposed-modules:
HsBindgen.Patterns
HsBindgen.ConstantArray
HsBindgen.Patterns
HsBindgen.Patterns.FlexibleArrayMember
HsBindgen.Patterns.LibC
other-modules:
HsBindgen.Patterns.Arithmetic
HsBindgen.Patterns.Backtrace
HsBindgen.Patterns.Enum.Bitfield
HsBindgen.Patterns.Enum.Simple
HsBindgen.Patterns.LibC.Arch
hs-source-dirs:
src
if arch(x86_64)
hs-source-dirs: src-x86_64
else
if arch(aarch64)
hs-source-dirs: src-aarch64
else
buildable: False
build-depends:
, pretty-show >= 1.10 && < 1.11
, vector ^>=0.13.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HsBindgen.Patterns.LibC.Arch (
) where
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HsBindgen.Patterns.LibC.Arch (
) where
Loading
Loading