3

I've written a ( not so ) small bash script to generate my generic makefiles. These makefiles detect the host architecture at the beginning, and create all objects and outputs in a folder with the arch as name.

it goes like this:

uname_m = $(shell uname -m)
DETECTED_ARCHITECTURE=$(uname_m)
ifeq ($(ARCH), x86_64)
    CFLAGS += -m64
    LDFLAGS += -m64
endif
ifeq ($(ARCH), x86)
    CFLAGS += -m32
    LDFLAGS += -m32
endif
OUTPUT_DIR = $(DETECTED_ARCHITECTURE)/
OBJ_PREFIX_DIR = $(OUTPUT_DIR)objs/

So for example, if i run make for my code in my work pc, it outputs everything to a folder named i686, at home to a folder x86_64, and on my raspberry, to armv6l. Also, if i do something like ARCH=i686 make, it correctly overrides the detected architecture so that it sets the correct compiler options, and output directory. The final output goes into OUTPUT_DIR, and all the generated .o files go into OUTPUT_DIR/objs.

I'm trying to do something similar with cmake. I've looked at CMake add_custom_command/_target in different directories for cross-compilation and CMAKE output directory depending on generator architecture, but i do not want the architecture to be dependent on the generator. I want it to be dependent on the compiling machine.

For example, i code in my pc, and compile it. if all goes well, i ssh to my raspberry, mount the code folder from my pc to the raspberry thru sshfs, and compile it there. Since everything goes to a different folder ( armv6l vs x86_64 ), there are no collisions, and i don't have to re-generate the makefile, or clean, or whatever.

So, basically, is it possible to tell cmake to make a makefile architecture aware? Thanks

Community
  • 1
  • 1
Joao Pincho
  • 939
  • 2
  • 11
  • 26
  • possible duplicate of [How to detect target architecture using CMake?](http://stackoverflow.com/questions/11944060/how-to-detect-target-architecture-using-cmake) – Chris Maes Nov 14 '14 at 13:31

1 Answers1

1

This is taken from the following e-mail thread: Determine-32-vs-64-bit-cpu

if(CMAKE_SIZEOF_VOID_P EQUAL 8) 
    set(arch_64 TRUE) 
else() 
    set(arch_64 FALSE) 
endif() 

And the following caveat was given

However, remember that on Mac OS X this is almost always the wrong thing to do when you're building universal binaries. So, if your software is supposed to work on Mac, you will need to take special precautions.

I have used this method with success.

If you aren't worried about Mac, this should work for you. If you are worried about Mac, you'll want to check if you are on a Mac and use the "fat" binary flags instead of 64 vs 32 flags.

EDIT

A better answer is given here: https://stackoverflow.com/a/12024211/1399279.

Specifically, check out his CMake implementation of this here: https://github.com/petroules/solar-cmake/blob/master/TargetArch.cmake

Community
  • 1
  • 1
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • That is able to guess correctly between 32 and 64 bits. but what about arm? It'll just fall inside the 32 bit section and fail miserably. My intention is to separate the build files according to architecture, not just pointer size. The makefile snippet i posted does just that. I could just try to "inject" that snippet of makefile inside a cmake script, for it to include in the makefile's creation. Is it possible? – Joao Pincho May 29 '13 at 15:50
  • @RhiakathFlanders Hmm... I hadn't thought of that. Unfortunately, you can't "inject" Makefile snippents through your CMakeLists.txt to my knowledge. Check out this answer for a rather roundabout method: http://stackoverflow.com/a/12024211/1399279 – SethMMorton May 29 '13 at 16:39
  • Sweet little jesus! that guy wrote the holy bible in cmake just to detect the architecture! But that's still not what i wanted... He is able to detect the architecture within cmake, but i want to do it within the makefile ( at compile time ). I guess it's just not possible with cmake. Cmake needs to know beforehand where it will place the output and what i want, is for the makefile to decide where it'll place it. So i could just share the code thru sshfs and use "make" without having to recreate the makefile all over again. So i would need cmake to inject that arch-detecting into the makefile. – Joao Pincho May 30 '13 at 11:08
  • Yeah, that won't be possible in CMake. The reason is that CMake is supposed to be able to create *any* build system, so if you could inject code directly into the Makefile it would break all other build systems that don't use Make syntax. You might try something else like `scons` or `waf` that allow you to write the configuration and build systems in one file. Best of luck. – SethMMorton May 30 '13 at 16:14