Skip to content
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cabal/src/Distribution/Simple/GHC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ linkGhcOptions verbosity lbi bi clbi =
(mkVersion [9, 4])
(compiler lbi)
(maybeToFlag $ programPath <$> lookupProgram gppProgram (withPrograms lbi))
, ghcOptAsProgram = maybeToFlag $ programPath <$> lookupProgram gccProgram (withPrograms lbi)
}
where
exe_paths =
Expand Down
3 changes: 3 additions & 0 deletions Cabal/src/Distribution/Simple/Program/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ data GhcOptions = GhcOptions
-- ^ Program to use for the C compiler; the @ghc -pgmc@ flag.
, ghcOptGppProgram :: Flag FilePath
-- ^ Program to use for the C++ compiler; the @ghc -pgmcxx@ flag.
, ghcOptAsProgram :: Flag FilePath
-- ^ Program to use for the Assembler; the @ghc -pgma@ flag.
, ----------------------------
-- Language and extensions

Expand Down Expand Up @@ -889,6 +891,7 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
, ["-opta" ++ opt | opt <- ghcOptAsmOptions opts]
, concat [["-pgmc", cc] | cc <- flag ghcOptCcProgram]
, concat [["-pgmcxx", cxx] | cxx <- flag ghcOptGppProgram]
, concat [["-pgma", as] | as <- flag ghcOptAsProgram]
, -----------------
-- Linker stuff

Expand Down
15 changes: 15 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsPgma/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C (CInt (..))

foreign import ccall "pgma.h meaning_of_life_asm"
meaning_of_life_asm :: IO CInt

main :: IO ()
main = do
secret <- meaning_of_life_asm
if secret == 33
then putStrLn ("The secret is " ++ show secret)
else error ("Expected value 33, got " ++ show secret)
7 changes: 7 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsPgma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ForeignOptsPgma

This test case asserts that cabal passes the `-pgma` GHC option to override the assembler program.

The cabal file sets `ghc-options: -pgma scripts/as-wrapper.sh`, pointing GHC at a shell script wrapper (`scripts/as-wrapper.sh`) instead of the system assembler program. The wrapper adds `--defsym meaning_of_life_val=33` to every compilation and then delegates to the real `as`. The assembler source requires `meaning_of_life_val` to be defined; if the wrapper is not used as the assembler program, the build fails.

This test is skipped on Windows (no POSIX shell).
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# macOS/AArch64 (Mach-O)
.macro func name
.globl _\name
_\name:
.endm

.text
func meaning_of_life_asm

mov w0, #MEANING_OF_LIFE_VAL
ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
section .text
global meaning_of_life_asm

meaning_of_life_asm:
mov eax, meaning_of_life_val
ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.text
.globl meaning_of_life_asm
.type meaning_of_life_asm, @function
meaning_of_life_asm:
movl $MEANING_OF_LIFE_VAL, %eax
ret
.size meaning_of_life_asm, .-meaning_of_life_asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.text
.globl meaning_of_life_asm

meaning_of_life_asm:
movl $MEANING_OF_LIFE_VAL, %eax
ret
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsPgma/abits/pgma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef PGMALIB_H
#define PGMALIB_H

int meaning_of_life_asm();

#endif
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test.Cabal.Prelude

main = do
cabalTest $ recordMode DoNotRecord $ do
cwd <- fmap testCurrentDir getTestEnv
let wrapper = cwd </> "scripts/as-wrapper.sh"
cabal "v2-build" ["foreign-opts-pgma-exe"]
withPlan $ runPlanExe "foreign-opts-pgma" "foreign-opts-pgma-exe" []
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cabal-version: 3.0
name: foreign-opts-pgma
version: 0.1
build-type: Simple

executable foreign-opts-pgma-exe
main-is: Main.hs
build-depends: base
default-language: Haskell2010
include-dirs: abits
if os(windows)
asm-sources: abits/asmlib_x86_64_windows.S
elif os(darwin)
asm-sources: abits/asmlib_x86_64_darwin.asm
asm-options: -d meaning_of_life_val=33
elif os(aarch64)
asm-sources: abits/asmlib_aarch64_darwin.S
elif os(linux)
asm-sources: abits/asmlib_x86_64_linux.S
ghc-options: -pgma scripts/as-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
# Wrapper around `as` that adds --d meaning_of_life_val=33 to every compilation.
# Used by the ForeignOptsPgma test to verify that -pgma selects this wrapper.
exec nasm -f macho64 "$@"
8 changes: 4 additions & 4 deletions cabal-testsuite/PackageTests/ShowBuildInfo/Complex/single.out

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion cabal-testsuite/src/Test/Cabal/OutputNormalizer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Data.Array ((!))
import Text.Regex.Base
import Text.Regex.TDFA

import qualified Data.Foldable as F
import Data.Foldable qualified as F

normalizeOutput :: NormalizerEnv -> String -> String
normalizeOutput nenv =
Expand Down Expand Up @@ -138,6 +138,10 @@ normalizeOutput nenv =
. resub
"\"-pgmcxx\",\"[^\"]+\""
"\"-pgmcxx\",\"<CXXCPATH>\""
-- Normalize the assembler compiler path embedded in -pgma.
. resub
"\"-pgma\",\"[^\"]+\""
"\"-pgma\",\"<ASPATH>\""
-- Remove cabal version output from show-build-info output
. resub
("{\"cabal-lib-version\":\"" ++ posixRegexEscape (display (normalizerCabalVersion nenv)) ++ "\"")
Expand Down
8 changes: 8 additions & 0 deletions changelog.d/11764.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
synopsis: Add explicit `pgma` flag for GHC invocation
packages: [Cabal]
prs: 11764
---
Add an explicit `pgma` flag to Cabal's GHC invocation logic. Pass the assembler
program explicitly to GHC. This change allows users to specify a custom `as`
program to compile assembly manually.
Loading