### If you want to edit this, copy it from cmake/share to cmake. It will be
### picked up in preference over the one in cmake/share. And it will not get
### clobbered with the next upgrade.

# can be included multiple times
# define OBJC_LOADER_INC
#        CREATE_OBJC_LOADER_INC
#        LIBRARY_NAME for multiple libraries
#

if( MULLE_TRACE_INCLUDE)
   message( STATUS "# Include \"${CMAKE_CURRENT_LIST_FILE}\"" )
endif()

# this is the second part, the option is in DefineLoaderIncObjC.cmake

if( CREATE_OBJC_LOADER_INC)
   if( NOT LIBRARY_NAME)
      set( LIBRARY_NAME "${PROJECT_NAME}")
   endif()

   #
   # Create src/objc-loader.inc for Objective-C projects. This contains a
   # list of all the classes and categories, contained in a library.
   #
   # runs in build dir
   if( NOT MULLE_OBJC_LOADER_TOOL)
      message( FATAL_ERROR "Executable \"mulle-objc-loader-tool\" not found")
   endif()

   if( NOT OBJC_LOADER_INC)
      set( OBJC_LOADER_INC "${CMAKE_SOURCE_DIR}/src/reflect/objc-loader.inc")
   endif()

   # add to headers being installed, not part of project headers though
   # because it is too late here
   set( PUBLIC_HEADERS
      ${PUBLIC_HEADERS}
      ${OBJC_LOADER_INC}
   )
   message( STATUS "OBJC_LOADER_INC is \"${OBJC_LOADER_INC}\"")

   if( INHERITED_OBJC_LOADERS)
     list( REMOVE_DUPLICATES INHERITED_OBJC_LOADERS)
   endif()

   message( STATUS "INHERITED_OBJC_LOADERS is \"${INHERITED_OBJC_LOADERS}\"")


   # The preferred way:
   #
   # _1_MulleFoundation is an object library (a collection of files).
   # _2_MulleFoundation is the loader with OBJC_LOADER_INC.
   #
   # Produce a static library _3_MulleFoundation from _1_MulleFoundation
   # to feed into MULLE_OBJC_LOADER_TOOL.
   #
   # The static library is, so that the commandline doesn't overflow for
   # many .o files.
   # In the end OBJC_LOADER_INC will be generated, which will be
   # included by the Loader.
   #
   if( TARGET "_2_${LIBRARY_NAME}")
      add_library( "_3_${LIBRARY_NAME}" STATIC
         $<TARGET_OBJECTS:_1_${LIBRARY_NAME}>
      )
      set( OBJC_LOADER_LIBRARY "$<TARGET_FILE:_3_${LIBRARY_NAME}>")

      set( STAGE2_HEADERS
         ${STAGE2_HEADERS}
         ${OBJC_LOADER_INC}
      )
   else()
      if( TARGET "_1_${LIBRARY_NAME}")
         message( FATAL_ERROR "_1_${LIBRARY_NAME} is defined, but _2_${LIBRARY_NAME} is missing.
   Maybe MulleObjCLoader+${LIBRARY_NAME}.h not part of STAGE2_SOURCES ?
   Tip: Check if \"$ENV{PROJECT_SOURCE_DIR}/MulleObjCLoader+${LIBRARY_NAME}.m\" (sic) exists")
      endif()
      set( OBJC_LOADER_LIBRARY "$<TARGET_FILE:${LIBRARY_NAME}>")
   endif()

   add_custom_command(
      OUTPUT ${OBJC_LOADER_INC}
      COMMAND ${MULLE_OBJC_LOADER_TOOL}
                 $ENV{MULLE_OBJC_LOADER_TOOL_FLAGS}
                 -c "${CMAKE_BUILD_TYPE}"
                 -o "${OBJC_LOADER_INC}"
                 ${OBJC_LOADER_LIBRARY}
                 ${INHERITED_OBJC_LOADERS}
      DEPENDS ${OBJC_LOADER_LIBRARY}
              ${ALL_LOAD_DEPENDENCY_LIBRARIES}
      COMMENT "Create: ${OBJC_LOADER_INC}"
      VERBATIM
   )

   #
   # if set to true, than a make clean/ninja clean removes it's
   # which we don't want... Doesn't help though...
   #
   # set_source_files_properties( "${OBJC_LOADER_INC}"
   #    PROPERTIES GENERATED FALSE
   # )

   add_custom_target( "__objc_loader_inc__"
      DEPENDS ${OBJC_LOADER_INC}
   )

   if( TARGET "_2_${LIBRARY_NAME}")
      add_dependencies( "_2_${LIBRARY_NAME}" "__objc_loader_inc__")
   else()
      add_dependencies( "${LIBRARY_NAME}" "__objc_loader_inc__")
   endif()

   # seemingly needed
   foreach( TMP_STAGE2_SOURCE in STAGE2_SOURCES)
      set_property( SOURCE ${TMP_STAGE2_SOURCE} APPEND PROPERTY OBJECT_DEPENDS ${OBJC_LOADER_INC})
   endforeach()

   #
   # tricky: this file can only be installed during link phase.
   #         Used by optimization.
   #
   if( LINK_PHASE)
      install( FILES "${OBJC_LOADER_INC}" DESTINATION "include/${LIBRARY_NAME}/private")
   endif()
endif()

include( CreateLoaderIncAuxObjC OPTIONAL)


# extension : mulle-objc/objc-cmake
# directory : project/all
# template  : .../CreateLoaderIncObjC.cmake
# Suppress this comment with `export MULLE_SDE_GENERATE_FILE_COMMENTS=NO`
