1

I try to add a custom compiler to CMake and I have been using this answer to create it: Generic rule from makefile to CMake

When I use enable_languange(MYCOMPILER) this works, but then I have to add also set_target_properties() to be able to compile everything.

I wanted that the user can write his project with just

project(MyProject MYCOMPILER)

The files are copied in a global directory "/opt/amiga/utils/CMake" and the path also includes "/opt/amiga/utils", so CMake is able to find and process the files, which I confirmed by putting message statements in there. but then I get an error from CMake:

CMake Error: Cannot determine link language for target "MyProject"

So what exactly is required that CMake is recognizing the linking stage as well?

CMakeDetermineAMIGA_CCompiler.cmake

set(AMIGA_COMPILER_NAME $ENV{CC})

if (NOT AMIGA_COMPILER_NAME)
    set(AMIGA_COMPILER_NAME m68k-amigaos-gcc)
endif()

find_path(AMIGA_COMPILER_PATH

    NAMES
    "${AMIGA_COMPILER_NAME}"
    "${AMIGA_COMPILER_NAME}.exe"

    CMAKE_FIND_ROOT_PATH_BOTH
)

if ("${AMIGA_COMPILER_PATH}" STREQUAL "AMIGA_COMPILER_PATH-NOTFOUND")
    message(FATAL_ERROR "'${AMIGA_COMPILER_NAME}' not found. Make sure that '${AMIGA_COMPILER_NAME}' is in PATH!")
endif ("${AMIGA_COMPILER_PATH}" STREQUAL "AMIGA_COMPILER_PATH-NOTFOUND")

# Remove the <path>/bin part
get_filename_component(AMIGA_PATH_PREFIX ${AMIGA_COMPILER_PATH} DIRECTORY)
set(AMIGA_PATH_PREFIX "${AMIGA_PATH_PREFIX}/")

find_program(
    AMIGA_COMPILER 
        NAMES
            "${AMIGA_COMPILER_NAME}" 
            "${AMIGA_COMPILER_NAME}.exe" 
        HINTS "${AMIGA_COMPILER_PATH}"
        DOC "gcc Amiga OS 1.x" 
)

if ("${AMIGA_COMPILER}" STREQUAL "AMIGA_COMPILER-NOTFOUND")
    message(FATAL_ERROR "'${AMIGA_COMPILER_NAME}' not found. Make sure that '${AMIGA_COMPILER_NAME}[.exe]' is in PATH!")
endif ("${AMIGA_COMPILER}" STREQUAL "AMIGA_COMPILER-NOTFOUND")
mark_as_advanced(CMAKE_${ASM_DIALECT}_COMPILER)

set(CMAKE_AMIGA_C_COMPILER "${AMIGA_COMPILER}")
set(CMAKE_AMIGA_C_SOURCE_FILE_EXTENSIONS c;C;cpp;cxx;CPP;CXX;cc;CC)
set(CMAKE_AMIGA_C_OUTPUT_EXTENSION .o)
set(CMAKE_AMIGA_C_COMPILER_ENV_VAR "CC")

# Configure variables set in this file for fast reload later on
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeAMIGA_CCompiler.cmake.in
               ${CMAKE_PLATFORM_INFO_DIR}/CMakeAMIGA_CCompiler.cmake)

CMakeAMIGA_CInformation.cmake

SET(BUILD_SHARED_LIBS OFF)

set(CMAKE_AMIGA_C_FLAGS "--save-temps -Wall -pedantic -mcrt=nix13 -fno-exceptions -fno-rtti")
set(CMAKE_AMIGA_C_FLAGS_DEBUG "${CMAKE_ASM_FLAGS} -D_DEBUG -g -mregparm")
set(CMAKE_AMIGA_C_FLAGS_RELEASE "${CMAKE_ASM_FLAGS} --O2 -mregparm")

set(CMAKE_INCLUDE_FLAG_AMIGA_C "-I")

if(NOT CMAKE_AMIGA_C_COMPILE_OBJECT)
    message(STATUS "Enable AMIGA_C")
    set(CMAKE_AMIGA_C_COMPILE_OBJECT "<CMAKE_AMIGA_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")
endif()

if(NOT CMAKE_AMIGA_C_CREATE_STATIC_LIBRARY)
    set(CMAKE_AMIGA_C_CREATE_STATIC_LIBRARY
        "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS> "
        "<CMAKE_RANLIB> <TARGET> ")
endif()

if(NOT DEFINED CMAKE_AMIGA_C_ARCHIVE_CREATE)
  set(CMAKE_AMIGA_C_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
endif()

if(NOT DEFINED CMAKE_AMIGA_C_ARCHIVE_APPEND)
  set(CMAKE_AMIGA_C_ARCHIVE_APPEND "<CMAKE_AR> q  <TARGET> <LINK_FLAGS> <OBJECTS>")
endif()

if(NOT DEFINED CMAKE_AMIGA_C_ARCHIVE_FINISH)
  set(CMAKE_AMIGA_C_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
endif() 

if(NOT CMAKE_AMIGA_C_LINK_EXECUTABLE)
  set(CMAKE_AMIGA_C_LINK_EXECUTABLE
    "<CMAKE_AMIGA_C_COMPILER> <FLAGS> <CMAKE_AMIGA_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS>  -o <TARGET> <LINK_LIBRARIES>")
endif()

set(CMAKE_AMIGA_C_INFORMATION_LOADED 1)

CMakeAMIGA_CCompiler.cmake.in

set(CMAKE_AMIGA_C_COMPILER "@CMAKE_AMIGA_C_COMPILER@")
set(CMAKE_AMIGA_C_COMPILER_LOADED 1)
set(CMAKE_AMIGA_C_SOURCE_FILE_EXTENSIONS @CMAKE_AMIGA_C_DIALECT_SOURCE_FILE_EXTENSIONS@)
set(CMAKE_AMIGA_C_OUTPUT_EXTENSION @CMAKE_AMIGA_C_OUTPUT_EXTENSION@)
set(CMAKE_AMIGA_C_COMPILER_ENV_VAR "@CMAKE_AMIGA_C_DIALECT_COMPILER_ENV_VAR@")

CMakeTestAMIGA_CCompiler.cmake

set(CMAKE_AMIGA_C_COMPILER_WORKS 1 CACHE INTERNAL "")

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(Test AMIGA_C)
set(TARGET ${PROJECT_NAME}) 

set(SOURCES
    main.cpp
)

add_executable(${TARGET} ${SOURCES})
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • Please post an [MCVE]. Please post the sources of the files you created and specify where you created them, so that others can reproduce the same error. I guess - did you set `CMAKE_MYCOMPILER_LINK_EXECUTABLE`? – KamilCuk Jan 01 '20 at 14:11
  • You are just using gcc, why not just specify a toolchain file? – KamilCuk Jan 01 '20 at 14:37
  • Because this is a special gcc which requires different parameters. Also I didn't want to use a toolchain. I already have a working setup, but I rather wanted to add this to the cmake system like a regular compiler which also doesn't need a toolchain parameter. – Devolus Jan 01 '20 at 14:38
  • `m68k-amigaos-gcc` is a normal gcc. Maybe just set `CMAKE_COMPILER_PREFIX` and include `CMakeDetermineCCompiler.cmake` (or something like that)? Why would go with [reinventing the wheel](https://github.com/Kitware/CMake/blob/master/Modules/CMakeCInformation.cmake#L177)? `my "own" compiler in CMake` - but you are not setuping your "compiler", you are setuping your language "AMIGA_C". It seems you are still compiling C++ sources, just set `CMAKE_CXX_COMPILER` to your own compiler and use the language to CXX. – KamilCuk Jan 01 '20 at 14:39
  • Yes, but it still requires a different setup. I also wanted to add other setups as well, so this is just one example. The question was not how to get it work, but how to setup my "own" compiler in CMake (which in this case might be some gcc port, but doesn't have to). – Devolus Jan 01 '20 at 14:41
  • Where would I set this? You mean in CMakeDetermineAMIGA_CCompiler? – Devolus Jan 01 '20 at 14:42
  • To provide a custom compiler with custom semantics that supports C++, you should aim to provide the platform files, like ex. [Linux-intel](https://github.com/Kitware/CMake/blob/master/Modules/Platform/Linux-Intel.cmake). – KamilCuk Jan 01 '20 at 14:45
  • Hmm. So I would have to create a "Amiga_C.cmake" platform file? And how do I enable this? Because when I just run 'cmake . -Bbuild-dir' it only finds the local compiler of the host, whcih of course is not what I need. I still would have the same problem though, when I try to create AMIGA_VASM language, though, or is there is some better alternative? – Devolus Jan 01 '20 at 14:47

0 Answers0