
FIND_PACKAGE(MFC)

if (NOT MFC_FOUND)
	message(STATUS "MFC could not be found, not building FRED")
	return()
endif()

set(FRED2_SOURCES
	altshipclassdlg.cpp
	altshipclassdlg.h
	fictionviewerdlg.cpp
	fictionviewerdlg.h
	addvariabledlg.cpp
	addvariabledlg.h
	addmodifycontainerdlg.cpp
	addmodifycontainerdlg.h
	adjustgriddlg.cpp
	adjustgriddlg.h
	asteroideditordlg.cpp
	asteroideditordlg.h
	bgbitmapdlg.cpp
	bgbitmapdlg.h
	briefingeditordlg.cpp
	briefingeditordlg.h
	calcrelativecoordsdlg.cpp
	calcrelativecoordsdlg.h
	campaigneditordlg.cpp
	campaigneditordlg.h
	campaignfilelistbox.cpp
	campaignfilelistbox.h
	campaigntreeview.cpp
	campaigntreeview.h
	campaigntreewnd.cpp
	campaigntreewnd.h
	checkboxlistdlg.cpp
	checkboxlistdlg.h
	cmdbrief.cpp
	cmdbrief.h
	createwingdlg.cpp
	createwingdlg.h
	customdatadlg.cpp
	customdatadlg.h
	customstringsdlg.cpp
	customstringsdlg.h
	customwingnames.cpp
	customwingnames.h
	debriefingeditordlg.cpp
	debriefingeditordlg.h
	dumpstats.cpp
	dumpstats.h
	editcontainernamedlg.cpp
	editcontainernamedlg.h
	editor.h
	eventeditor.cpp
	eventeditor.h
	fred.cpp
	fred.h
	freddoc.cpp
	freddoc.h
	fredrender.cpp
	fredrender.h
	fredstubs.cpp
	fredview.cpp
	fredview.h
	grid.cpp
	grid.h
	ignoreordersdlg.cpp
	ignoreordersdlg.h
	initialships.cpp
	initialships.h
	initialstatus.cpp
	initialstatus.h
	jumpnodedlg.cpp
	jumpnodedlg.h
	listitemchooser.cpp
	listitemchooser.h
	mainfrm.cpp
	mainfrm.h
	management.cpp
	management.h
	messageeditordlg.cpp
	messageeditordlg.h
	missioncutscenesdlg.cpp
	missioncutscenesdlg.h
	missiongoalsdlg.cpp
	missiongoalsdlg.h
	missionnotesdlg.cpp
	missionnotesdlg.h
	modifyvariabledlg.cpp
	modifyvariabledlg.h
	musicplayerdlg.cpp
	musicplayerdlg.h
	operatorargtypeselect.cpp
	operatorargtypeselect.h
	OperatorComboBox.cpp
	OperatorComboBox.h
	orienteditor.cpp
	orienteditor.h
	playerstarteditor.cpp
	playerstarteditor.h
	prefsdlg.cpp
	prefsdlg.h
	propdlg.cpp
	propdlg.h
	reinforcementeditordlg.cpp
	reinforcementeditordlg.h
	resource.h
	restrictpaths.cpp
	restrictpaths.h
	setglobalshipflags.cpp
	setglobalshipflags.h
	sexp_tree.cpp
	sexp_tree.h
	shieldsysdlg.cpp
	shieldsysdlg.h
	ship_select.cpp
	ship_select.h
	shipchecklistbox.cpp
	shipchecklistbox.h
	shipclasseditordlg.cpp
	shipclasseditordlg.h
	shipeditordlg.cpp
	shipeditordlg.h
	shipflagsdlg.cpp
	shipflagsdlg.h
	shipgoalsdlg.cpp
	shipgoalsdlg.h
	shipspecialdamage.cpp
	shipspecialdamage.h
	shipspecialhitpoints.cpp
	shipspecialhitpoints.h
	shiptexturesdlg.cpp
	shiptexturesdlg.h
	soundenvironmentdlg.cpp
	soundenvironmentdlg.h
	supportrearmdlg.cpp
	supportrearmdlg.h
	starfieldeditor.cpp
	starfieldeditor.h
	stdafx.cpp
	stdafx.h
	textviewdlg.cpp
	textviewdlg.h
	voiceactingmanager.cpp
	voiceactingmanager.h
	volumetricsdlg.cpp
	volumetricsdlg.h
	warpparamsdlg.cpp
	warpparamsdlg.h
	waypointpathdlg.cpp
	waypointpathdlg.h
	weaponeditordlg.cpp
	weaponeditordlg.h
	wing.cpp
	wing.h
	wing_editor.cpp
	wing_editor.h
	MFCGraphicsOperations.cpp
	MFCGraphicsOperations.h
	fred.rc
	version.rc2
)

set_source_files_properties(fred.rc PROPERTIES
	LANGUAGE RC
)

# This should be kept in sync with the corresponding IF() in cmake/toolchain-msvc.cmake
IF(MSVC_USE_RUNTIME_DLL OR FSO_BUILD_QTFRED)
	set(CMAKE_MFC_FLAG 2)
ELSE()
	set(CMAKE_MFC_FLAG 1)
ENDIF()

IF(MSVC60)
	link_directories(${STLPORT_INCLUDE_LIB_DIRS})
ENDIF(MSVC60)

add_executable(FRED2 WIN32 ${FRED2_SOURCES})

target_compile_features(FRED2 PUBLIC cxx_std_17)

SET_TARGET_PROPERTIES(FRED2 PROPERTIES OUTPUT_NAME "fred2_open_${FSO_BINARY_SUFFIX}")

target_compile_definitions(FRED2 PUBLIC FRED NO_WARN_MBCS_MFC_DEPRECATION)

if (MSVC)
	set_target_properties(FRED2 PROPERTIES LINK_FLAGS "/FORCE:MULTIPLE")
endif()

INCLUDE_DIRECTORIES(${FREESPACE_HEADERS})
INCLUDE_DIRECTORIES(${GENERATED_SOURCE_DIR})

IF(USE_STLPORT)
	INCLUDE_DIRECTORIES(BEFORE SYSTEM ${STLPORT_INCLUDE_DIRS})
ENDIF(USE_STLPORT)

FIND_PACKAGE(OpenGL REQUIRED)
target_link_libraries(FRED2 PUBLIC "${OPENGL_LIBRARY}")

TARGET_LINK_LIBRARIES(FRED2 PUBLIC code) # This will also link all dependencies of code

# When CMAKE_MFC_FLAG=2 (DLL MFC) MSBuild already links the DLL MFC stub libs (mfcs*.lib);
# explicitly adding the static MFC libs (nafxcw*) on top causes IsDerivedFrom to be resolved
# from the static MFC copy while CRuntimeClass structs are initialized in the DLL form,
# crashing CDocTemplate's ctor on startup.
IF(NOT (MSVC_USE_RUNTIME_DLL OR FSO_BUILD_QTFRED))
	TARGET_LINK_LIBRARIES(FRED2 PUBLIC "$<$<NOT:$<CONFIG:Release>>:nafxcwd.lib>")
	TARGET_LINK_LIBRARIES(FRED2 PUBLIC "$<$<CONFIG:Release>:nafxcw.lib>")
ENDIF()

target_link_libraries(FRED2 PUBLIC glad_wgl)

enable_clang_tidy(FRED2)

INSTALL(
	TARGETS FRED2
	RUNTIME DESTINATION ${BINARY_DESTINATION}
)
if (FSO_INSTALL_DEBUG_FILES)
	if (MSVC)
		install(FILES "$<TARGET_PDB_FILE:FRED2>"
				DESTINATION ${BINARY_DESTINATION}
				OPTIONAL)
	endif()
endif()

INCLUDE(util)
COPY_FILES_TO_TARGET(FRED2)

include(CreateLaunchers)
create_target_launcher(FRED2
	WORKING_DIRECTORY ${FSO_FREESPACE_PATH}
	ARGS ${FSO_RUN_ARGUMENTS})
