Page 1 of 1

Changing compiler and assembler in CMake

Posted: Mon Aug 29, 2016 11:05 am
by psnix
Hi,

I intend to use gcc-cross-compiler for my OS, but I don't know how to change default compiler (gcc) to i686-elf-gcc and default assembler to i686-elf-as in CMake (CMakeLists.txt)

How can I change default assembler and compiler in CMake?

Re: Changing compiler and assembler in CMake

Posted: Mon Aug 29, 2016 12:36 pm
by Lowl3v3l
In general the proper way to do it is a toolchain file which sets the proper variables. additionally you COULD set the compiler and stuff via commandline, but this will likely fail as the crosscompiler is likely not able to compile cmake's test programs.

Re: Changing compiler and assembler in CMake

Posted: Tue Aug 30, 2016 12:25 am
by psnix
Writing make files is quite challenging, however working with CMake for OS development seems to be more tricky, isn't it? shall I use pure Make instead of CMake for developing my OS?

Re: Changing compiler and assembler in CMake

Posted: Tue Aug 30, 2016 1:24 am
by onlyonemac
Personally I just use a standard makefile. No automake, no cmake. I've got a few scripts though to help me compile binaries using the correct compiler configuration and linkscripts - that way, I don't have to repeat a whole series of commands in each make target; I can just say "./scripts/compile.sh ..." and "./scripts/link.sh ..." wherever I need to compile or link something.

Re: Changing compiler and assembler in CMake

Posted: Tue Aug 30, 2016 9:34 am
by kzinti
I got CMake to cross compile my kernel, but it wasn't a pleasant experience and I would recommend staying away from CMake. In any case, here are the toolchain files I came up with:

i686-elf-gcc toolchain (toolchain-i686.cmake):

Code: Select all

SET(CMAKE_SYSTEM_NAME Kiznix)
SET(CMAKE_SYSTEM_PROCESSOR x86)
SET(CMAKE_SYSTEM_VERSION 1)

INCLUDE(CMakeForceCompiler)
CMAKE_FORCE_C_COMPILER(i686-elf-gcc GNU)

SET(CMAKE_C_SIZEOF_DATA_PTR 4)

SET(CMAKE_C_FLAGS "-O2 -g -ffreestanding -fbuiltin -Wall -Wextra -std=gnu99 -msse" CACHE STRING "" FORCE)

x86_64-elf-gcc toolchain (toolchain-x86_64.cmake):

Code: Select all

SET(CMAKE_SYSTEM_NAME Kiznix)
SET(CMAKE_SYSTEM_PROCESSOR x86_64)
SET(CMAKE_SYSTEM_VERSION 1)

INCLUDE(CMakeForceCompiler)
CMAKE_FORCE_C_COMPILER(x86_64-elf-gcc GNU)

SET(CMAKE_C_SIZEOF_DATA_PTR 8)

SET(CMAKE_C_FLAGS "-O2 -g -ffreestanding -fbuiltin -Wall -Wextra -std=gnu99 -mcmodel=kernel -mno-red-zone" CACHE STRING "" FORCE)
You can then invoke cmake with something like this:

Code: Select all

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/toolchain-x86_64.cmake $(PROJECT_DIR)