Changing compiler and assembler in CMake

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Changing compiler and assembler in CMake

Post 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?
Lowl3v3l
Posts: 8
Joined: Mon Aug 29, 2016 12:33 pm
Libera.chat IRC: Lowl3v3l

Re: Changing compiler and assembler in CMake

Post 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.
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: Changing compiler and assembler in CMake

Post 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?
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Changing compiler and assembler in CMake

Post 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.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Changing compiler and assembler in CMake

Post 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)
Post Reply