#
# This file is part of AtomVM.
#
# Copyright 2025 Paul Guyot <pguyot@kallisys.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

project(jit)

include(BuildErlang)

set(ERLANG_MODULES
    jit
    jit_precompile
    jit_regs
    jit_stream_binary
    jit_stream_flash
    jit_stream_mmap
    jit_aarch64
    jit_aarch64_asm
    jit_arm32
    jit_arm32_asm
    jit_armv6m
    jit_armv6m_asm
    jit_armv7m_asm
    jit_riscv32
    jit_riscv32_asm
    jit_riscv64
    jit_riscv64_asm
    jit_wasm32
    jit_wasm32_asm
    jit_x86_64
    jit_x86_64_asm
    jit_xtensa
    jit_xtensa_asm
)

include(../../../version.cmake)

set(JIT_PRECOMPILED_MODULES
    jit
    jit_regs
    jit_stream_binary
    jit_stream_flash
    jit_stream_mmap
    jit_@ARCH@
    jit_@ARCH@_asm
)

if (NOT AVM_DISABLE_JIT_DWARF)
    list(INSERT ERLANG_MODULES 1 jit_dwarf)
    list(INSERT JIT_PRECOMPILED_MODULES 1 jit_dwarf)
    set(erlc_flags -DJIT_DWARF -DATOMVM_VERSION=\"${ATOMVM_BASE_VERSION}\" -pa ${CMAKE_CURRENT_BINARY_DIR}/beams)
else()
    set(erlc_flags)
    # Compile jit_dwarf.beam for BEAM tests even when DWARF is disabled (not packed in .avm)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/beams/jit_dwarf.beam
        COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/beams
            && erlc +debug_info
                -o ${CMAKE_CURRENT_BINARY_DIR}/beams
                -I ${CMAKE_SOURCE_DIR}/libs/include
                -I ${CMAKE_SOURCE_DIR}/libs
                -I ${CMAKE_CURRENT_SOURCE_DIR}/../include
                ${CMAKE_CURRENT_SOURCE_DIR}/jit_dwarf.erl
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jit_dwarf.erl
        COMMENT "Compiling jit_dwarf.erl (BEAM tests only, not packed in .avm)"
        VERBATIM
    )
endif()

pack_precompiled_archive(jit ERLC_FLAGS ${erlc_flags} MODULES ${ERLANG_MODULES} PRECOMPILED_MODULES ${JIT_PRECOMPILED_MODULES})

if (AVM_DISABLE_JIT_DWARF)
    add_custom_target(jit_dwarf_for_beam_tests ALL
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/beams/jit_dwarf.beam
    )
endif()

if (NOT AVM_DISABLE_JIT_DWARF)
    # jit_dwarf_pt is a parse transform needed to compile jit.beam; it is not packed
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/beams/jit_dwarf_pt.beam
        COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/beams
            && erlc +debug_info
                -o ${CMAKE_CURRENT_BINARY_DIR}/beams
                -I ${CMAKE_SOURCE_DIR}/libs/include
                -I ${CMAKE_SOURCE_DIR}/libs
                -I ${CMAKE_CURRENT_SOURCE_DIR}/../include
                ${CMAKE_CURRENT_SOURCE_DIR}/jit_dwarf_pt.erl
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jit_dwarf_pt.erl
        COMMENT "Compiling jit_dwarf_pt.erl"
        VERBATIM
    )
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/beams/jit.beam
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/beams/jit_dwarf_pt.beam
        APPEND
    )
endif()

# jit_riscv32.erl and jit_riscv64.erl include the shared implementation in
# jit_riscv_impl.hrl, so changes to that header must force recompilation.
foreach(riscv_module jit_riscv32 jit_riscv64)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/beams/${riscv_module}.beam
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jit_riscv_impl.hrl
        APPEND
    )
endforeach()

# The register-based backends include the shared native-register bookkeeping in
# jit_backend_regs_impl.hrl, so changes to that header must force recompilation.
foreach(regs_module jit_x86_64 jit_aarch64 jit_arm32 jit_armv6m jit_xtensa jit_riscv32 jit_riscv64)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/beams/${regs_module}.beam
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jit_backend_regs_impl.hrl
        APPEND
    )
endforeach()

set(JIT_VERSION ${ATOMVM_BASE_VERSION})

install(
    DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/beams/
    DESTINATION lib/atomvm/lib/jit-${JIT_VERSION}/ebin
    FILES_MATCHING PATTERN "*.beam"
)

install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/jit.avm
    DESTINATION lib/atomvm/lib/jit-${JIT_VERSION}/ebin/
)

install(
    DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
    DESTINATION lib/atomvm/lib/jit-${JIT_VERSION}/src
    FILES_MATCHING PATTERN "*.erl"
)

install(
    DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../include
    DESTINATION lib/atomvm/lib/jit-${JIT_VERSION}/include
    FILES_MATCHING PATTERN "*.hrl"
)
