##
#   Copyright (C) 2004 - 2026 Université de Versailles Saint-Quentin-en-Yvelines (UVSQ)
#
#   This file is part of MAQAO.
#
#  MAQAO is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public License
#   as published by the Free Software Foundation; either version 3
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##

IF (APPLE)
   SET(_HAVE_APPLE_ 1)
ENDIF (APPLE)
IF (UNIX)
   SET(_HAVE_UNIX_ 1)
ENDIF (UNIX)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libelf.h.cmake ${CMAKE_CURRENT_SOURCE_DIR}/libelf.h)

FILE(GLOB_RECURSE
   file_troll
   elfprinter.c
   libmtroll.c
)
FILE(GLOB_RECURSE
   file_elf
   ar_reader.c
   libelf.c
)

SET(LIBRARY_OUTPUT_PATH       "${CMAKE_CURRENT_SOURCE_DIR}")

### --- Create the elf objects --- ###
# Create the elf object for STATIC libraries/binaries #
ADD_LIBRARY(elf-obj-static                   OBJECT  ${file_elf})
SET_TARGET_PROPERTIES(elf-obj-static         PROPERTIES COMPILE_FLAGS "${C_STATIC_FLAGS}")

IF (is_WINDOWS)
	# Create the elf object for DYNAMIC libraries/binaries #
	ADD_LIBRARY(elf-obj-dyn                      OBJECT  ${file_elf})
	SET_TARGET_PROPERTIES(elf-obj-dyn            PROPERTIES COMPILE_FLAGS "${C_DYNAMIC_FLAGS}")
ENDIF(is_WINDOWS)

### --- Create the mtroll objects --- ###
# Create the mtroll object for STATIC libraries/binaries #
ADD_LIBRARY(mtroll-obj-static                OBJECT ${file_troll})
SET_TARGET_PROPERTIES(mtroll-obj-static      PROPERTIES COMPILE_FLAGS "${C_STATIC_FLAGS}")

IF (is_WINDOWS)
	# Create the mtroll object for DYNAMIC libraries/binaries #
	ADD_LIBRARY(mtroll-obj-dyn                   OBJECT ${file_troll})
	SET_TARGET_PROPERTIES(mtroll-obj-dyn         PROPERTIES COMPILE_FLAGS "${C_DYNAMIC_FLAGS}")
ENDIF(is_WINDOWS)

### --- Create the static elf library --- ###
ADD_LIBRARY(elf-lib-static STATIC            $<TARGET_OBJECTS:elf-obj-static>)
SET_TARGET_PROPERTIES(elf-lib-static         PROPERTIES OUTPUT_NAME elf)

### --- Install the headers --- ###
FILE(COPY libelf.h               DESTINATION ${INCLUDE_OUTPUT_PATH})
FILE(COPY libmtroll.h            DESTINATION ${INCLUDE_OUTPUT_PATH})
FILE(COPY elf.h                  DESTINATION ${INCLUDE_OUTPUT_PATH})
FILE(COPY ar.h                   DESTINATION ${INCLUDE_OUTPUT_PATH})
FILE(COPY ar_reader.h            DESTINATION ${INCLUDE_OUTPUT_PATH})

### --- For cleaning files generated by cmake --- ###
ADD_CUSTOM_TARGET(distclean_libtroll
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_SOURCE_DIR}/libelf.h
    )

