2011-06-02 31 views
6

我在makefile以下位:如何檢測GHC是否設置爲默認生成32位或64位代碼?

GLFW_FLAG := -m32 -O2 -Iglfw/include -Iglfw/lib -Iglfw/lib/cocoa $(CFLAGS) 
... 
$(BUILD_DIR)/%.o : %.c 
    $(CC) -c $(GLFW_FLAG) $< -o [email protected] 
$(BUILD_DIR)/%.o : %.m 
    $(CC) -c $(GLFW_FLAG) $< -o [email protected] 

-m32指示GCC生成32位代碼。這是因爲在某些配置中,GHC被設置爲構建32位代碼,但GCC的默認設置有時是64位。我想概括一下,以便自動檢測GHC是在構建32位還是64位代碼,然後將正確的標誌傳遞給GCC。

問題:我該如何問GHC它將構建什麼類型的代碼(32位與64位)?

PS:在編譯期間,我的cabal文件調用此makefile以解決cabal中的解決方法侷限性。我希望我可以在我的cabal文件中將它們列爲c源代碼。

+1

我可以使用它從GHC中獲取字的大小,但我不知道它是否使用正確的值:WORDSIZE:= $(shell ghc + RTS --info | ghc -e「do c < - getContents; let {r = read c :: [(String,String)]; wsize = Data.Maybe.fromJust(lookup \「Word size \」r)}; putStrLn wsize「) – 2011-06-02 17:02:01

+0

編譯一個測試程序是否合理並用Elf庫(在hackage上)讀取結果? – 2011-06-02 17:44:48

+0

你可以使用'ghc -e'print(maxBound :: Int)''的結果,它應該取決於你是否有32位或64位的GHC ... – hvr 2011-06-02 19:38:47

回答

2

感謝Ed'ka現在我知道正確的答案。

的Makefile中現在有這樣一個規則:

GCCFLAGS := $(shell ghc --info | ghc -e "fmap read getContents >>= putStrLn . unwords . read . Data.Maybe.fromJust . lookup \"Gcc Linker flags\"") 

這是一個有點長,但它的作用是提取GHC的輸出「GCC連接標誌」。 注:這是ghc --info的輸出而不是ghc +RTS --info

這是比其他建議的方式更好,因爲它給了我所有需要指定,而不是僅僅-m標誌的標誌。當不需要標誌時它也是空的。

謝謝大家!

+0

似乎你需要像'getContents >> = putStrLn。 Data.Maybe.fromJust。查找「C編譯器鏈接標誌」。 read' – 2015-05-13 00:26:01

1

根據我的評論,應該可以編譯一個測試程序並讀取生成的二進制文件。

$ cabal install elf 

,代碼只是

readElf testFile'sPath >>= \e -> if elfClass e == ELFCLASS64 then print 64 else print 32 

或者在GHCI我們看到:

> e <- readElf "/bin/ls" 
> elfClass e 
ELFCLASS64 
5

慣用的伎倆我看到的是要求在一個Int字節或位大小或者Word,因爲這根據機器的字大小在GHC中變化,所以

Prelude> :m + Foreign 
Prelude Foreign> sizeOf (undefined :: Int) 
8 
Prelude Foreign> bitSize (undefined :: Int) 
64 

或者使用的系統工具:

$ cat A.hs 
main = print() 

$ ghc --make A.hs 
[1 of 1] Compiling Main    (A.hs, A.o) 
Linking A ... 

$ file A 
A: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), 
    dynamically linked (uses shared libs), for GNU/Linux 2.6.27, not stripped