
include_directories(. ../.. ../includes ${SDL2_INCLUDE_DIRS} softfloat
                    ${CMAKE_CURRENT_BINARY_DIR})

# The sources generated by gencpu:
set(CPUEMU_SRCS cpustbl.c cpuemu_0.c cpuemu_11.c cpuemu_13.c
		cpuemu_20.c cpuemu_21.c cpuemu_22.c cpuemu_23.c cpuemu_24.c
		cpuemu_31.c cpuemu_32.c cpuemu_33.c cpuemu_34.c cpuemu_35.c
		cpuemu_40.c cpuemu_50.c)

# Sources that are synchronized with WinUAE:
set(WINUAE_SRCS cpudefs.c cpummu.c cpummu030.c debug.c disasm.c newcpu_common.c newcpu.c
		readcpu.c writelog.c fpp.c fpp_native.c fpp_softfloat.c
		softfloat/softfloat.c softfloat/softfloat_decimal.c
		softfloat/softfloat_fpsp.c machdep/m68k.c)

# Unfortunately we've got to specify the rules for the generated files twice,
# once for cross compiling (with calling the host cc directly) and once
# for native compiling so that the rules also work for non-Unix environments...
if(CMAKE_CROSSCOMPILING)

	add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build68k
		COMMAND cc -I${CMAKE_CURRENT_SOURCE_DIR}
			   ${CMAKE_CURRENT_SOURCE_DIR}/build68k.c
			   ${CMAKE_CURRENT_SOURCE_DIR}/writelog.c
			   -o ${CMAKE_CURRENT_BINARY_DIR}/build68k
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/build68k.c)

	add_custom_command(OUTPUT cpudefs.c
		COMMAND ./build68k < ${CMAKE_CURRENT_SOURCE_DIR}/table68k >cpudefs.c
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/table68k
			${CMAKE_CURRENT_BINARY_DIR}/build68k)

	add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gencpu
		COMMAND cc -I${CMAKE_CURRENT_SOURCE_DIR}
			   cpudefs.c ${CMAKE_CURRENT_SOURCE_DIR}/gencpu.c
			   ${CMAKE_CURRENT_SOURCE_DIR}/readcpu.c
			   -o ${CMAKE_CURRENT_BINARY_DIR}/gencpu
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gencpu.c
			${CMAKE_CURRENT_SOURCE_DIR}/readcpu.c cpudefs.c)

	add_custom_command(OUTPUT ${CPUEMU_SRCS}
		COMMAND ${CMAKE_CURRENT_BINARY_DIR}/gencpu
		DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/gencpu)

else()	# Rules for normal build follow

	add_executable(build68k build68k.c writelog.c)

	add_custom_command(OUTPUT cpudefs.c
		COMMAND build68k < ${CMAKE_CURRENT_SOURCE_DIR}/table68k >cpudefs.c
		DEPENDS table68k build68k)

	add_executable(gencpu gencpu.c readcpu.c cpudefs.c)

	add_custom_command(OUTPUT ${CPUEMU_SRCS} COMMAND gencpu  DEPENDS gencpu)

endif(CMAKE_CROSSCOMPILING)

if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
	# Silence some warnings for the autogenerated sources - the generated
	# cpuemu_xx.c contains a lot of warnings we don't really care about...
	set(CPUEMU_CFLAGS "-Wno-sign-compare -Wno-unused-variable")
	set(CPUEMU_CFLAGS "${CPUEMU_CFLAGS} -fwrapv")
	if (WARN_SHADOW_LOCAL_AVAILABLE)
		set(CPUEMU_CFLAGS "${CPUEMU_CFLAGS} -Wno-shadow=local")
	endif()
	check_c_compiler_flag(-Wunused-but-set-variable HAS_WUNUSED_BUT_SET_VAR)
	if (HAS_WUNUSED_BUT_SET_VAR)
		set (CPUEMU_CFLAGS "${CPUEMU_CFLAGS} -Wno-unused-but-set-variable")
	endif()
	set_source_files_properties(${CPUEMU_SRCS}
		PROPERTIES COMPILE_FLAGS ${CPUEMU_CFLAGS})

	# For the other files, hide some harmless warnings, too (since we
	# try to keep the files in sync with WinUAE, it's hard to fix them)
	set(CPUMAIN_CFLAGS "-Wno-unused-variable -Wno-unused-function -Wno-unused-label")
	set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-missing-braces -Wno-sign-compare")
	set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -fwrapv")
	if (WARN_FALLTRHOUGH_AVAILABLE)
		set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-implicit-fallthrough")
	endif()
	if (WARN_SHADOW_LOCAL_AVAILABLE)
		set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-shadow=local")
	endif()
	if (HAS_WUNUSED_BUT_SET_VAR)
		set (CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-unused-but-set-variable")
	endif()
	# The remaining warnings should be hidden for release builds:
	if (CMAKE_BUILD_TYPE STREQUAL "Release")
		set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-bad-function-cast")
		if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.7)
			set(CPUMAIN_CFLAGS "${CPUMAIN_CFLAGS} -Wno-maybe-uninitialized")
		endif()
	endif()
	set_source_files_properties(${WINUAE_SRCS}
		PROPERTIES COMPILE_FLAGS ${CPUMAIN_CFLAGS})
endif()

add_library(UaeCpu ${CPUEMU_SRCS} ${WINUAE_SRCS} custom.c events.c memory.c
		   hatari-glue.c)

target_link_libraries(UaeCpu PRIVATE ${SDL2_LIBRARIES})
