简体   繁体   English

如何检测 GHC 默认设置为生成 32 位还是 64 位代码?

[英]How can I detect if GHC is set to generate 32bit or 64bit code by default?

I have the following bits in my makefile :我的makefile中有以下位:

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

The -m32 instructs GCC to generate 32bit code. -m32指示 GCC 生成 32 位代码。 It's there because on some configurations GHC is set to build 32bit code but GCC's default is sometimes 64bit.它存在是因为在某些配置中,GHC 设置为构建 32 位代码,但 GCC 的默认值有时是 64 位。 I would like to generalize this so that it autodetects whether GHC is building 32bit or 64bit code and then passes the correct flag to GCC.我想概括一下,以便它自动检测 GHC 是在构建 32 位还是 64 位代码,然后将正确的标志传递给 GCC。

Question : How can I ask GHC what type of code (32bit vs. 64bit) it will build?问题:我如何询问 GHC 它将构建什么类型的代码(32 位与 64 位)?

PS: My cabal file calls this makefile during the build to workaround limitations in cabal. PS:我的 cabal 文件在构建期间调用此 makefile 以解决 cabal 中的限制。 I do wish I could just list these as c-sources in my cabal file.我真希望我可以在我的 cabal 文件中将这些列为 c 源。

The usual trick I see is to ask for the size in bytes or bits of an Int or Word , since this varies in GHC based on the word size of the machine,我看到的常用技巧是询问IntWord的字节或位大小,因为这在 GHC 中会根据机器的字大小而有所不同,

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

Or use system tools:或者使用系统工具:

$ 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

Thanks to Ed'ka I know the correct answer now.感谢 Ed'ka,我现在知道正确答案了。

The Makefile now has a rule like this: Makefile 现在有这样的规则:

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

It's a bit long, but all it does is extract the "Gcc Linker flags" from ghc's output.它有点长,但它所做的只是从 ghc 的 output 中提取“Gcc Linker 标志”。 Note: This is the output of ghc --info and not ghc +RTS --info .注意:这是ghc --info的 output 而不是ghc +RTS --info

This is better than the other suggested ways as it gives me all the flags that need to be specified and not just the -m flag.这比其他建议的方式更好,因为它给了我所有需要指定的标志,而不仅仅是 -m 标志。 It's also empty when no flags are needed.当不需要标志时它也是空的。

Thank you everyone!谢谢大家!

As per my comment, it should be possible to compile a test program and read the resulting binary.根据我的评论,应该可以编译测试程序并读取生成的二进制文件。

$ cabal install elf

And the code is just代码只是

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

Or in GHCi we see:或者在 GHCi 中我们看到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM