1function(add_loader_object name)
2  cmake_parse_arguments(
3    "ADD_LOADER_OBJECT"
4    "ALIAS" # Option argument
5    "SRC"   # Single value arguments
6    "DEPENDS;COMPILE_OPTIONS" # Multi value arguments
7    ${ARGN}
8  )
9
10  get_fq_target_name(${name} fq_target_name)
11  get_fq_deps_list(fq_deps_list ${ADD_LOADER_OBJECT_DEPENDS})
12  if(ADD_LOADER_OBJECT_ALIAS)
13    list(LENGTH ADD_LOADER_OBJECT_DEPENDS deps_size)
14    if(NOT (${deps_size} EQUAL "1"))
15      message(FATAL_ERROR "A loader object alias should have exactly one dependency.")
16    endif()
17    list(GET ADD_LOADER_OBJECT_DEPENDS 0 dep)
18    get_fq_dep_name(fq_dep_name ${dep})
19
20    add_custom_target(${fq_target_name})
21    add_dependencies(${fq_target_name} ${fq_dep_name})
22    get_target_property(dep_objfile ${fq_dep_name} OBJECT_FILES)
23    set_target_properties(
24      ${fq_target_name}
25      PROPERTIES
26        "TARGET_TYPE" "${OBJECT_LIBRARY_TARGET_TYPE}"
27        "OBJECT_FILES" ""
28        "DEPS" "${fq_dep_name}"
29    )
30    return()
31  endif()
32
33  add_object_library(
34    ${name}.__objects__
35    SRCS ${ADD_LOADER_OBJECT_SRC}
36    DEPENDS ${ADD_LOADER_OBJECT_DEPENDS}
37    COMPILE_OPTIONS ${ADD_LOADER_OBJECT_COMPILE_OPTIONS}
38  )
39
40  set(objfile ${LIBC_BUILD_DIR}/lib/${name}.o)
41  add_custom_command(
42    OUTPUT ${objfile}
43    COMMAND cp $<TARGET_OBJECTS:${fq_target_name}.__objects__> ${objfile}
44    DEPENDS $<TARGET_OBJECTS:${fq_target_name}.__objects__>
45  )
46  add_custom_target(
47    ${fq_target_name}
48    DEPENDS ${objfile}
49  )
50  set_target_properties(
51    ${fq_target_name}
52    PROPERTIES
53      "TARGET_TYPE" "${OBJECT_LIBRARY_TARGET_TYPE}"
54      "OBJECT_FILES" ""
55      "DEPS" "${fq_target_name}.__objects__"
56  )
57endfunction()
58
59if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE}))
60  message(STATUS "Skipping loader for target machine ${LIBC_TARGET_MACHINE}")
61  return()
62endif()
63
64add_subdirectory(${LIBC_TARGET_MACHINE})
65
66add_loader_object(
67  crt1
68  ALIAS
69  DEPENDS
70    .${LIBC_TARGET_MACHINE}.crt1
71)
72