zillemarco / CppPlug

4 stars 0 forks source link

Compile errors with Mono2.0 Unity3d #1

Open roscopecoltran opened 7 years ago

roscopecoltran commented 7 years ago

Hi,

Again :-) Hope you are all well !

I used polly for passing a toolchain (repo: https://github.com/ruslo/polly), and changed the path $MONO_ROOT to use Unity3D's Mono 2.0 framework (build environment: Apple)

It triggers the an error due to ManagedCalculator target not being defined. Please see below, the details of the build setup.

CMake Error at tests/ManagedCalculator/CMakeLists.txt:10 (set_property):
  set_property could not find TARGET ManagedCalculator.  Perhaps it has not
  yet been created.

CMake Error at tests/ManagedCalculator/CMakeLists.txt:12 (add_dependencies):
  Cannot add target-level dependencies to non-existent target
  "ManagedCalculator".

  The add_dependencies works for top-level logical targets created by the
  add_executable, add_library, or add_custom_target commands.  If you want to
  add file-level dependencies see the DEPENDS option of the add_custom_target
  and add_custom_command commands.

To fix it, i added the following in ./tests/ManagedCalculator/CMakeLists.txt

add_library(${PROJECT_NAME} SHARED ${SRC})

but it triggers a missing dependency target error.

CMake Error at CMakeLists.txt:92 (add_dependencies):
  The dependency target "CppPlug.Net" of target "CppPlug" does not exist.

And, after some hacks and tests, too lengthy to explain, I got a series of error related to some mono constants not defined (eg. MONO_DEFAULT_DOMAIN_NAME, MONO_DEFAULT_ASSEMBLY_DIR,...).

My assumption is that Apple MacOSX build env are not covered and tested yet.

3rdparty installed:

install polly:

git clone --depth=1 https://github.com/ruslo/polly ~/dev/cmake/toolchains/polly
echo "POLLY_ROOT=\"~/dev/cmake/toolchains/polly\"" >> ~/.bash_profile
echo "PATH=$PATH:$POLLY_ROOT/bin"  >> ~/.bash_profile
source ~/.bash_profile

**default build command***

rm -fR ./_* 
cmake -H. -B_builds \
                  -DMANAGED_PLUGINS_SUPPORT=ON \
                  -DCREATE_INSTALL=OFF 
                  -DBUILD_TESTS=ON 
                  -DMONO_ROOT=/Volumes/HardDrive/Applications/Unity/MonoDevelop.app/Contents/Frameworks/Mono.framework/Versions/Current
cmake --build _builds/

build command with Polly:

build.py --toolchain=osx-10-12 \
                  --config Release 
                  --install 
                  --clear 
                  --reconfig 
                  --fwd MANAGED_PLUGINS_SUPPORT=ON \
                             CREATE_INSTALL=OFF \ 
                             BUILD_TESTS=ON \ 
                             MONO_ROOT=/Volumes/HardDrive/Applications/Unity/MonoDevelop.app/Contents/Frameworks/Mono.framework/Versions/Current

My changes:

#include_directories("${MONO_ROOT}/include/mono-2.0")
- include_directories("${MONO_ROOT}/share/mono-2.0")

#set(MONO_RUNTIME_DLL "${MONO_ROOT}/bin/monosgen-2.0.dll")
set(MONO_RUNTIME_DLL "${MONO_ROOT}/bin/mono-sgen")

#target_link_libraries(${PROJECT_NAME} "${MONO_ROOT}/lib/monosgen-2.0.lib")
target_link_libraries(${PROJECT_NAME} "${MONO_ROOT}/lib/libmonosgen-2.0.1.dylib")

CMakeLists.txt used:

cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11)

project(CppPlug C CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Check target architecture
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(CppPlug_64_BIT 1)
else()
    set(CppPlug_64_BIT 0)
endif()

# 

set(CppPlug_VERSION_MAJOR "0")
set(CppPlug_VERSION_MINOR "1")
set(CppPlug_VERSION_PATCH "0")
set(CppPlug_VERSION "${CppPlug_VERSION_MAJOR}.${CppPlug_VERSION_MINOR}.${CppPlug_VERSION_PATCH}")

option(MANAGED_PLUGINS_SUPPORT  "If enabled managed plugins will be supported using Mono"    OFF)
option(CREATE_INSTALL           "Generate installation target"                               OFF)
option(BUILD_TESTS              "Builds the tests"                                           ON)

add_definitions(-DCppPlug_EXPORT_SHARED)

if(MANAGED_PLUGINS_SUPPORT)
set(MONO_ROOT "" CACHE STRING "Path where to find the Mono installation")

if(MONO_ROOT STREQUAL "")
    message(FATAL_ERROR "Provide a valid root path for Mono")
endif()

add_definitions(-DSUPPORT_MANAGED)
include_directories("${MONO_ROOT}/share/mono-2.0")
#include_directories("${MONO_ROOT}/include/mono-2.0")

set(MONO_COMPILER_DIR "${MONO_ROOT}/bin")
endif()

file(GLOB SRC "${PROJECT_SOURCE_DIR}/src/*.*")

add_library(${PROJECT_NAME} SHARED ${SRC})

if(WIN32)
    set_property(TARGET ${PROJECT_NAME} PROPERTY COMPILE_DEFINITIONS_RELEASE _CRT_SECURE_NO_WARNINGS)
    set_property(TARGET ${PROJECT_NAME} PROPERTY COMPILE_DEFINITIONS_DEBUG _CRT_SECURE_NO_WARNINGS)
endif()

if(MANAGED_PLUGINS_SUPPORT)

# mono-sgen

# /Volumes/HardDrive/Applications/Unity/MonoDevelop.app/Contents/Frameworks/Mono.framework/Versions/Current/lib/libmonosgen-2.0.1.dylib
# /monosgen/
set(MONO_RUNTIME_DLL "${MONO_ROOT}/bin/mono-sgen")
#set(MONO_RUNTIME_DLL "${MONO_ROOT}/bin/monosgen-2.0.dll")
target_link_libraries(${PROJECT_NAME} "${MONO_ROOT}/lib/libmonosgen-2.0.1.dylib")

#target_link_libraries(${PROJECT_NAME} "${MONO_ROOT}/lib/monosgen-2.0.lib")

endif()

# Set the output path and properties
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG             "${CMAKE_BINARY_DIR}/Build/CppPlug/lib/Debug"           LIBRARY_OUTPUT_DIRECTORY_DEBUG          "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/Debug"           RUNTIME_OUTPUT_DIRECTORY_DEBUG          "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/Debug")
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE           "${CMAKE_BINARY_DIR}/Build/CppPlug/lib/Release"         LIBRARY_OUTPUT_DIRECTORY_RELEASE        "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/Release"         RUNTIME_OUTPUT_DIRECTORY_RELEASE        "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/Release")
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL        "${CMAKE_BINARY_DIR}/Build/CppPlug/lib/MinSizeRel"      LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL     "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/MinSizeRel"      RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL     "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/MinSizeRel")
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO    "${CMAKE_BINARY_DIR}/Build/CppPlug/lib/RelWithDebInfo"  LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/RelWithDebInfo"  RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/RelWithDebInfo")

set_target_properties(${PROJECT_NAME} PROPERTIES RELEASE_POSTFIX        "")
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX          "")
set_target_properties(${PROJECT_NAME} PROPERTIES MINSIZEREL_POSTFIX     "")
set_target_properties(${PROJECT_NAME} PROPERTIES RELWITHDEBINFO_POSTFIX "")

# Include version information in the output
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${CppPlug_VERSION})

if(MANAGED_PLUGINS_SUPPORT)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${MONO_RUNTIME_DLL}" "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>$<$<CONFIG:RelWithDebInfo>:RelWithDebInfo>$<$<CONFIG:MinSizeRel>:MinSizeRel>")
endif()

# CMake Error at CMakeLists.txt:102 (add_dependencies):
#  The dependency target "CppPlug.Net" of target "CppPlug" does not exist.

if (APPLE)
    # Append -fno-common to the compile flags to work around a bug in
    # Apple's GCC
    get_target_property(CppPlug_CFLAGS ${PROJECT_NAME} COMPILE_FLAGS)

    if (NOT CppPlug_CFLAGS)
        set(CppPlug_CFLAGS "")
    endif()

    set_target_properties(${PROJECT_NAME} PROPERTIES
                        COMPILE_FLAGS "${CppPlug_CFLAGS} -fno-common"
                        INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/CppPlug/lib${LIB_SUFFIX}")

    set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${CppPlug_VERSION})
endif()

if(MANAGED_PLUGINS_SUPPORT)
    add_subdirectory(src/dotNet)

    add_dependencies(${PROJECT_NAME} "CppPlug.Net")

    if(WIN32)
        if(NOT EXISTS "${PROJECT_SOURCE_DIR}/CppPlugGen/build/CppPlugGen.sln")
            message(FATAL_ERROR "CppPlugGen project wans't found. Generate the project using the script inside the CppPlugGen folder")
        endif()

        if(CppPlug_64_BIT)
        else()
            set(CppPlug_Net_Bindings "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/")
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND "${PROJECT_SOURCE_DIR}/CppPlugGen/build/lib/CppPlugGen.exe" "${MONO_COMPILER_DIR}" "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>$<$<CONFIG:RelWithDebInfo>:RelWithDebInfo>$<$<CONFIG:MinSizeRel>:MinSizeRel>/" WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
        endif()
    endif()
endif()

if(BUILD_TESTS)
    add_subdirectory(tests/Calculator)
    add_subdirectory(tests/StandardCalculator)
    add_subdirectory(tests/AdvancedCalculator)

    if(MANAGED_PLUGINS_SUPPORT)
        add_subdirectory(tests/ManagedCalculator)
    endif()
endif()

if(CREATE_INSTALL)

    if(MANAGED_PLUGINS_SUPPORT)
        install(FILES "${MONO_RUNTIME_DLL}" DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
    endif()

    install(DIRECTORY "${PROJECT_SOURCE_DIR}/src/" DESTINATION "include" PATTERN "*.cpp" EXCLUDE PATTERN "dotNet" EXCLUDE)

    if(MANAGED_PLUGINS_SUPPORT)
        install(FILES "${CMAKE_BINARY_DIR}/Build/CppPlug/bin/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>$<$<CONFIG:RelWithDebInfo>:RelWithDebInfo>$<$<CONFIG:MinSizeRel>:MinSizeRel>/CppPlug.Net.dll" DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
    endif()

    install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "bin/"
                                    LIBRARY DESTINATION "bin/"
                                    ARCHIVE DESTINATION "lib/")
endif()

It would be awesome to get it work on Apple machines and to hook additional task to CppPlug as I mentioned in this issue.

Have a great week end ! :--)

Richard

zillemarco commented 7 years ago

Hi, again :) Unfortunately, like you've guessed I haven't added support to Apple's MacOS, yet. I'll be adding that for sure and if you need it I'll try to add it as soon as I can because right now I'm quite busy at work. Like I said, if you need it soon I'll find the time to add it sooner :)

roscopecoltran commented 7 years ago

Thanks for the quick reply !

I am using only a Mac, so if I want to go further, it would awesome to get Apple support handled. :-)

I cannot compile CppPlugGen too: error:

==== Building CppSharp.Runtime (release) ====
==== Building CppSharp.CppParser (release) ====
==== Building CppSharp.Parser.CSharp (release) ====
make: Circular CppSharp <- CppSharp dependency dropped.
make: Circular CppSharp.Generator <- CppSharp dependency dropped.
==== Building CppSharp.AST (release) ====
make: Circular CppSharp.Parser <- CppSharp dependency dropped.
==== Building CppSharp.Parser (release) ====
make[1]: *** No rule to make target `../lib/CppSharp.dll', needed by `../lib/CppSharp.Parser.dll'.  Stop.
make: *** [CppSharp.Parser] Error 2

build commands used:

git clone --recursive --depth=1 https://github.com/zillemarco/CppPlugGen ~/CppPlugGen
cd ~/CppPlugGen
[LLVM is downloaded and extracted...]
CppSharp/build/premake5-osx --file=premake5.lua gmake
config=release make -C build

My Mono VM is 32-bit, so to CppSharp, for example, I use: config=release_x32 make -C gmake

zillemarco commented 7 years ago

Yeah, CppPlugGen doesn't support Apple too unfortunately. By the way, you should check out CppSharp, more specifically CppSharp's command line interface (CppSharp.CLI) because that is what CppPlugGen has become. I'm currently working on making CppSharp.CLI compatible with Apple, so once I've finished it I'll make CppPlug Apple-compatible too (I need CppSharp.CLI to make CppPlug support .NET plugins).