Skip to content

Deduplicate hsc2hs args #11005

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
183 changes: 93 additions & 90 deletions Cabal/src/Distribution/Simple/PreProcess.hs
Original file line number Diff line number Diff line change
Expand Up @@ -518,101 +518,104 @@ ppHsc2hs bi lbi clbi =
-- directly, or via a response file.
genPureArgs :: Version -> ConfiguredProgram -> String -> String -> [String]
genPureArgs hsc2hsVersion gccProg inFile outFile =
-- Additional gcc options
[ "--cflag=" ++ opt
| opt <-
programDefaultArgs gccProg
++ programOverrideArgs gccProg
]
++ [ "--lflag=" ++ opt
| opt <-
programDefaultArgs gccProg
++ programOverrideArgs gccProg
]
-- OSX frameworks:
++ [ what ++ "=-F" ++ opt
| isOSX
, opt <- nub (concatMap Installed.frameworkDirs pkgs)
, what <- ["--cflag", "--lflag"]
]
++ [ "--lflag=" ++ arg
| isOSX
, opt <- map getSymbolicPath (PD.frameworks bi) ++ concatMap Installed.frameworks pkgs
, arg <- ["-framework", opt]
]
-- Note that on ELF systems, wherever we use -L, we must also use -R
-- because presumably that -L dir is not on the normal path for the
-- system's dynamic linker. This is needed because hsc2hs works by
-- compiling a C program and then running it.

++ ["--cflag=" ++ opt | opt <- platformDefines lbi]
-- Options from the current package:
++ ["--cflag=-I" ++ u dir | dir <- PD.includeDirs bi]
++ [ "--cflag=-I" ++ u (buildDir lbi </> unsafeCoerceSymbolicPath relDir)
| relDir <- mapMaybe symbolicPathRelative_maybe $ PD.includeDirs bi
]
++ [ "--cflag=" ++ opt
| opt <-
PD.ccOptions bi
++ PD.cppOptions bi
-- hsc2hs uses the C ABI
-- We assume that there are only C sources
-- and C++ functions are exported via a C
-- interface and wrapped in a C source file.
-- Therefore we do not supply C++ flags
-- because there will not be C++ sources.
--
-- DO NOT add PD.cxxOptions unless this changes!
]
++ [ "--cflag=" ++ opt
| opt <-
[ "-I" ++ u (autogenComponentModulesDir lbi clbi)
, "-I" ++ u (autogenPackageModulesDir lbi)
, "-include"
, u $ autogenComponentModulesDir lbi clbi </> makeRelativePathEx cppHeaderName
]
]
++ [ "--lflag=-L" ++ u opt
| opt <-
if withFullyStaticExe lbi
then PD.extraLibDirsStatic bi
else PD.extraLibDirs bi
]
++ [ "--lflag=-Wl,-R," ++ u opt
| isELF
, opt <-
if withFullyStaticExe lbi
then PD.extraLibDirsStatic bi
else PD.extraLibDirs bi
]
++ ["--lflag=-l" ++ opt | opt <- PD.extraLibs bi]
++ ["--lflag=" ++ opt | opt <- PD.ldOptions bi]
-- Options from dependent packages
++ [ "--cflag=" ++ opt
| pkg <- pkgs
, opt <-
["-I" ++ opt | opt <- Installed.includeDirs pkg]
++ Installed.ccOptions pkg
]
++ [ "--lflag=" ++ opt
| pkg <- pkgs
, opt <-
["-L" ++ opt | opt <- Installed.libraryDirs pkg]
++ [ "-Wl,-R," ++ opt | isELF, opt <- Installed.libraryDirs pkg
]
++ [ "-l" ++ opt
| opt <-
if withFullyStaticExe lbi
then Installed.extraLibrariesStatic pkg
else Installed.extraLibraries pkg
]
++ Installed.ldOptions pkg
]
cflags
++ ldflags
++ preccldFlags
++ hsc2hsOptions bi
++ postccldFlags
++ ["-o", outFile, inFile]
where
ldflags =
map ("--lflag=" ++) $
ordNub $
concat
[ programDefaultArgs gccProg ++ programOverrideArgs gccProg
, osxFrameworkDirs
, [ arg
| isOSX
, opt <- map getSymbolicPath (PD.frameworks bi) ++ concatMap Installed.frameworks pkgs
, arg <- ["-framework", opt]
]
, -- Note that on ELF systems, wherever we use -L, we must also use -R
-- because presumably that -L dir is not on the normal path for the
-- system's dynamic linker. This is needed because hsc2hs works by
-- compiling a C program and then running it.

-- Options from the current package:
[ "-L" ++ u opt
| opt <-
if withFullyStaticExe lbi
then PD.extraLibDirsStatic bi
else PD.extraLibDirs bi
]
, [ "-Wl,-R," ++ u opt
| isELF
, opt <-
if withFullyStaticExe lbi
then PD.extraLibDirsStatic bi
else PD.extraLibDirs bi
]
, ["-l" ++ opt | opt <- PD.extraLibs bi]
, PD.ldOptions bi
, -- Options from dependent packages
[ opt
| pkg <- pkgs
, opt <-
["-L" ++ opt | opt <- Installed.libraryDirs pkg]
++ [ "-Wl,-R," ++ opt | isELF, opt <- Installed.libraryDirs pkg
]
++ [ "-l" ++ opt
| opt <-
if withFullyStaticExe lbi
then Installed.extraLibrariesStatic pkg
else Installed.extraLibraries pkg
]
++ Installed.ldOptions pkg
]
]

cflags =
map ("--cflag=" ++) $
ordNub $
concat
[ programDefaultArgs gccProg ++ programOverrideArgs gccProg
, osxFrameworkDirs
, platformDefines lbi
, -- Options from the current package:
["-I" ++ u dir | dir <- PD.includeDirs bi]
, [ "-I" ++ u (buildDir lbi </> unsafeCoerceSymbolicPath relDir)
| relDir <- mapMaybe symbolicPathRelative_maybe $ PD.includeDirs bi
]
, -- hsc2hs uses the C ABI
-- We assume that there are only C sources
-- and C++ functions are exported via a C
-- interface and wrapped in a C source file.
-- Therefore we do not supply C++ flags
-- because there will not be C++ sources.
--
-- DO NOT add PD.cxxOptions unless this changes!
PD.ccOptions bi ++ PD.cppOptions bi
,
[ "-I" ++ u (autogenComponentModulesDir lbi clbi)
, "-I" ++ u (autogenPackageModulesDir lbi)
, "-include"
, u $ autogenComponentModulesDir lbi clbi </> makeRelativePathEx cppHeaderName
]
, -- Options from dependent packages
[ opt
| pkg <- pkgs
, opt <-
["-I" ++ opt | opt <- Installed.includeDirs pkg]
++ Installed.ccOptions pkg
]
]

osxFrameworkDirs =
[ "-F" ++ opt
| isOSX
, opt <- ordNub (concatMap Installed.frameworkDirs pkgs)
]

-- hsc2hs flag parsing was wrong
-- (see -- https://github.com/haskell/hsc2hs/issues/35)
-- so we need to put -- --cc/--ld *after* hsc2hsOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- |
-- Module: Foo
-- Copyright: (c) Sergey Vinokurov 2025
-- License: Apache-2.0 (see LICENSE)
-- Maintainer: serg.foo@gmail.com

{-# LANGUAGE CPP #-}

module Foo (foo) where

import Foo01
import Foo02
import Foo03
import Foo04
import Foo05
import Foo06
import Foo07
import Foo08
import Foo09
import Foo10

#include <include_Foo01.h>
#include <include_Foo02.h>
#include <include_Foo03.h>
#include <include_Foo04.h>
#include <include_Foo05.h>
#include <include_Foo06.h>
#include <include_Foo07.h>
#include <include_Foo08.h>
#include <include_Foo09.h>
#include <include_Foo10.h>
#include <include_FooDep01.h>
#include <include_FooDep02.h>
#include <include_FooDep03.h>
#include <include_FooDep04.h>
#include <include_FooDep05.h>
#include <include_FooDep06.h>
#include <include_FooDep07.h>
#include <include_FooDep08.h>
#include <include_FooDep09.h>
#include <include_FooDep10.h>
#include <include_FooDepDep01.h>
#include <include_FooDepDep02.h>
#include <include_FooDepDep03.h>
#include <include_FooDepDep04.h>
#include <include_FooDepDep05.h>
#include <include_FooDepDep06.h>
#include <include_FooDepDep07.h>
#include <include_FooDepDep08.h>
#include <include_FooDepDep09.h>
#include <include_FooDepDep10.h>

foo :: Int
foo = sum
[ #{const TEST_OPTION}
, foo01
, foo02
, foo03
, foo04
, foo05
, foo06
, foo07
, foo08
, foo09
, foo10

, #{const DEF_foo01}
, #{const DEF_foo02}
, #{const DEF_foo03}
, #{const DEF_foo04}
, #{const DEF_foo05}
, #{const DEF_foo06}
, #{const DEF_foo07}
, #{const DEF_foo08}
, #{const DEF_foo09}
, #{const DEF_foo10}

, #{const DEF_fooDep01}
, #{const DEF_fooDep02}
, #{const DEF_fooDep03}
, #{const DEF_fooDep04}
, #{const DEF_fooDep05}
, #{const DEF_fooDep06}
, #{const DEF_fooDep07}
, #{const DEF_fooDep08}
, #{const DEF_fooDep09}
, #{const DEF_fooDep10}

, #{const DEF_fooDepDep01}
, #{const DEF_fooDepDep02}
, #{const DEF_fooDepDep03}
, #{const DEF_fooDepDep04}
, #{const DEF_fooDepDep05}
, #{const DEF_fooDepDep06}
, #{const DEF_fooDepDep07}
, #{const DEF_fooDepDep08}
, #{const DEF_fooDepDep09}
, #{const DEF_fooDepDep10}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main (main) where

import Foo

main :: IO ()
main = putStrLn $ "Result = " ++ show foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
packages:
my-toplevel.cabal
deps/my01/my01.cabal
deps/my02/my02.cabal
deps/my03/my03.cabal
deps/my04/my04.cabal
deps/my05/my05.cabal
deps/my06/my06.cabal
deps/my07/my07.cabal
deps/my08/my08.cabal
deps/my09/my09.cabal
deps/my10/my10.cabal

deps/my-dep01/my-dep01.cabal
deps/my-dep02/my-dep02.cabal
deps/my-dep03/my-dep03.cabal
deps/my-dep04/my-dep04.cabal
deps/my-dep05/my-dep05.cabal
deps/my-dep06/my-dep06.cabal
deps/my-dep07/my-dep07.cabal
deps/my-dep08/my-dep08.cabal
deps/my-dep09/my-dep09.cabal
deps/my-dep10/my-dep10.cabal

deps/my-dep-dep01/my-dep-dep01.cabal
deps/my-dep-dep02/my-dep-dep02.cabal
deps/my-dep-dep03/my-dep-dep03.cabal
deps/my-dep-dep04/my-dep-dep04.cabal
deps/my-dep-dep05/my-dep-dep05.cabal
deps/my-dep-dep06/my-dep-dep06.cabal
deps/my-dep-dep07/my-dep-dep07.cabal
deps/my-dep-dep08/my-dep-dep08.cabal
deps/my-dep-dep09/my-dep-dep09.cabal
deps/my-dep-dep10/my-dep-dep10.cabal
Loading
Loading