Skip to content

Commit

Permalink
Merge pull request #347 from well-typed/c-types-prelude
Browse files Browse the repository at this point in the history
Add minimal `LibC` module
  • Loading branch information
TravisCardwell authored Jan 9, 2025
2 parents 8221616 + 7f5ff69 commit 1611562
Show file tree
Hide file tree
Showing 891 changed files with 59,169 additions and 68 deletions.
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

0 comments on commit 1611562

Please sign in to comment.