Add support for using cmake in the mono runtime build. (#41316)
authorZoltan Varga <vargaz@gmail.com>
Thu, 27 Aug 2020 14:13:41 +0000 (10:13 -0400)
committerGitHub <noreply@github.com>
Thu, 27 Aug 2020 14:13:41 +0000 (10:13 -0400)
* Add support for using cmake in the mono runtime build.

Not enabled by default. Ninja is used for building if available, else we fall
back to make.

* Build and install libmonosgen-2.0.dylib.

* Address review comments.

* Add a few more MONO_EMPTY_SOURCE_FILE defines to silence ranlib warnings on osx.

* Pass -mmacosx-version-min=10.12 on osx.

* Add eventpipe support.

* Quiet the OSX build a bit.

* Address review comments.

src/mono/CMakeLists.txt
src/mono/cmake/QuietOSXRanlib.cmake [new file with mode: 0644]
src/mono/mono.proj
src/mono/mono/metadata/filewatcher.c
src/mono/mono/metadata/w32file.c
src/mono/mono/metadata/w32process-unix-default.c
src/mono/mono/mini/CMakeLists.txt
src/mono/mono/utils/mono-dl-wasm.c
src/mono/mono/utils/mono-threads-wasm.c

index 1f2cc45..0c85c04 100644 (file)
@@ -8,10 +8,13 @@ include(CheckFunctionExists)
 include(TestBigEndian)
 include(FindPkgConfig)
 
+include("cmake/QuietOSXRanlib.cmake")
+
 set(CMAKE_MODULE_PATH
   ${CMAKE_MODULE_PATH}
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
   )
+set(CMAKE_INSTALL_MESSAGE LAZY)
 
 find_program(CCACHE_PROGRAM ccache)
 if(CCACHE_PROGRAM)
@@ -46,7 +49,11 @@ if(ENABLE_INTERP_LIB)
 endif()
 
 if(NOT DISABLE_INTERPRETER AND NOT DISABLE_JIT)
-set(ENABLE_ILGEN 1)
+  set(ENABLE_ILGEN 1)
+endif()
+
+if(NOT DISABLE_EVENTPIPE)
+  set(ENABLE_PERFTRACING 1)
 endif()
 
 # FIXME:
diff --git a/src/mono/cmake/QuietOSXRanlib.cmake b/src/mono/cmake/QuietOSXRanlib.cmake
new file mode 100644 (file)
index 0000000..7d45dbf
--- /dev/null
@@ -0,0 +1,20 @@
+if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
+  # Quiet 'file ... has no symbols' messages from ranlib
+  find_program(CMAKE_XCRUN NAMES xcrun)
+  execute_process(COMMAND ${CMAKE_XCRUN} -find libtool
+    OUTPUT_VARIABLE CMAKE_LIBTOOL
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+  get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
+  set(LIBTOOL_NO_WARNING_FLAG "-no_warning_for_no_symbols")
+  foreach(lang ${languages})
+    set(CMAKE_${lang}_CREATE_STATIC_LIBRARY
+      "\"${CMAKE_LIBTOOL}\" -static ${LIBTOOL_NO_WARNING_FLAG} -o <TARGET> <LINK_FLAGS> <OBJECTS>")
+  endforeach()
+  # Another instance
+  set(MONO_RANLIB "${PROJECT_BINARY_DIR}/mono-ranlib")
+  file(WRITE ${MONO_RANLIB} "#!/bin/sh\n")
+  file(APPEND ${MONO_RANLIB} "${CMAKE_RANLIB} -no_warning_for_no_symbols $*")
+  execute_process(COMMAND chmod a+x ${MONO_RANLIB})
+  set(CMAKE_RANLIB "${MONO_RANLIB}")
+endif()
+
index d0107b4..ad59322 100644 (file)
@@ -1,5 +1,10 @@
 <Project Sdk="Microsoft.Build.Traversal" DefaultTargets="Build">
 
+  <!--
+      Build properties:
+      - MonoEnableCMake - enable the cmake build
+  -->
+
   <PropertyGroup>
     <MonoCrossDir Condition="'$(MonoCrossDir)' == '' and '$(ROOTFS_DIR)' != ''">$(ROOTFS_DIR)</MonoCrossDir>
     <MonoEnableInterpreter Condition="'$(MonoEnableInterpreter)' == ''">false</MonoEnableInterpreter>
@@ -27,7 +32,7 @@
     <XcodeDir Condition="'$(XcodeDir)' == ''">/Applications/Xcode.app/Contents/Developer</XcodeDir>
     <BuildMonoAOTCrossCompiler Condition="'$(TargetsiOS)' == 'true'">true</BuildMonoAOTCrossCompiler>
     <BuildMonoAOTCrossCompiler Condition="'$(TargetstvOS)' == 'true'">true</BuildMonoAOTCrossCompiler>
-    <BuildMonoAOTCrossCompiler Condition="'$(TargetsBrowser)' == 'true'">true</BuildMonoAOTCrossCompiler>
+    <BuildMonoAOTCrossCompiler Condition="'$(TargetsBrowser)' == 'true' and '$(MonoEnableCMake)' != 'true'">true</BuildMonoAOTCrossCompiler>
     <CrossConfigH Condition="'$(BuildMonoAOTCrossCompiler)' == 'true'">$(MonoObjDir)cross\config.h</CrossConfigH>
     <MonoBundleLLVMOptimizer Condition="'$(MonoEnableLLVM)' == 'true'">true</MonoBundleLLVMOptimizer>
     <MonoCCompiler>$(Compiler)</MonoCCompiler>
     <Touch Condition="'$(_MonoAotCrossConfigureCommand)' != ''" Files="$(MonoObjDir)cross/config.h" />
   </Target>
 
-  <Target Name="BuildMonoRuntimeUnix" Condition="'$(OS)' != 'Windows_NT'" DependsOnTargets="ConfigureMonoRuntimeUnix">
+  <Target Name="BuildMonoRuntimeUnix" Condition="'$(OS)' != 'Windows_NT' and '$(MonoEnableCMake)' != 'true'" DependsOnTargets="ConfigureMonoRuntimeUnix">
     <Message Text="--- Building Mono ---" Importance="High" />
     <Exec Condition="'$(TargetsBrowser)' != 'true'" Command="make -j$([System.Environment]::ProcessorCount)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)" />
     <Exec Condition="'$(TargetsBrowser)' == 'true'" Command="bash -c 'source $(EMSDK_PATH)/emsdk_env.sh &amp;&amp; make -j$([System.Environment]::ProcessorCount)'" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)" />
   </Target>
 
   <!-- Windows Mono runtime build -->
-  <Target Name="BuildMonoRuntimeWindows" Condition="'$(OS)' == 'Windows_NT'">
+  <Target Name="BuildMonoRuntimeWindows" Condition="'$(OS)' == 'Windows_NT' and '$(MonoEnableCMake)' != 'true'">
 
     <!-- Sanity checks -->
     <Error Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'" Text="Error: Mono runtime for $(TargetOS) can't be built on Windows." />
     <Error Condition="!Exists('$(LocalDotnet)')" Text="'$(LocalDotnet)' doesn't exist." />
   </Target>
 
+  <Target Name="BuildMonoRuntimeCMake" Condition="'$(MonoEnableCMake)' == 'true'">
+    <Exec Command="which ninja" IgnoreExitCode="true" IgnoreStandardErrorWarningFormat="true">
+      <Output TaskParameter="ExitCode" PropertyName="MonoNinjaFound"/>
+    </Exec>
+    <PropertyGroup>
+      <MonoBuildTool Condition="'$(MonoNinjaFound)' == '0'">ninja</MonoBuildTool>
+      <MonoBuildTool Condition="'$(MonoNinjaFound)' != '0'">make -j$([System.Environment]::ProcessorCount)</MonoBuildTool>
+    </PropertyGroup>
+    <ItemGroup>
+      <MonoCMakeArgs Condition="'$(MonoBuildTool)' == 'ninja'" Include="-G Ninja"/>
+      <MonoCMakeArgs Include="-DCMAKE_INSTALL_PREFIX=$(MonoObjDir)out"/>
+    </ItemGroup>
+    <ItemGroup Condition="'$(TargetsOSX)' == 'true'">
+      <MonoCMakeArgs Include="-DCMAKE_OSX_DEPLOYMENT_TARGET=$(macOSVersionMin)" />
+    </ItemGroup>
+    <ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
+      <MonoCMakeArgs Include="-DENABLE_MINIMAL=ssa,com,jit,reflection_emit_save,portability,assembly_remapping,attach,verifier,full_messages,appdomains,shadowcopy,security,sgen_marksweep_conc,sgen_split_nursery,sgen_gc_bridge,sgen_toggleref,logging,remoting,shared_perfcounters,sgen_debug_helpers,sgen_binary_protocol,soft_debug,interpreter,assert_messages,cleanup,mdb,gac,threads,eventpipe,aot,interpreter"/>
+      <MonoCMakeArgs Include="-DENABLE_INTERP_LIB=1"/>
+      <MonoCMakeArgs Include="-DDISABLE_ICALL_TABLES=1"/>
+      <MonoCMakeArgs Include="-DDISABLE_CRASH_REPORTING=1"/>
+      <MonoCMakeArgs Include="-DENABLE_ICALL_EXPORT=1"/>
+      <MonoCMakeArgs Include="-DENABLE_LAZY_GC_THREAD_CREATION=1"/>
+      <MonoCMakeArgs Include="-DCMAKE_C_FLAGS=-I$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)/runtimes/browser-wasm/native/include"/>
+    </ItemGroup>
+    <MakeDir Directories="$(MonoObjDir)" />
+    <Message Text="Running cmake with arguments: @(MonoCMakeArgs, ' ') $(MonoProjectRoot)" Importance="High"/>
+    <!-- cmake -->
+    <Exec Condition="'$(TargetsBrowser)' != 'true'" Command="cmake @(MonoCMakeArgs, ' ') $(MonoProjectRoot)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)"/>
+    <Exec Condition="'$(TargetsBrowser)' == 'true'" Command="bash -c 'source $(EMSDK_PATH)/emsdk_env.sh &amp;&amp; emcmake cmake @(MonoCMakeArgs, ' ') $(MonoProjectRoot)'" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)" />
+    <!-- ninja/make -->
+    <Exec Condition="'$(TargetsBrowser)' != 'true'" Command="$(MonoBuildTool) all install" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)"/>
+    <Exec Condition="'$(TargetsBrowser)' == 'true'" Command="bash -c 'source $(EMSDK_PATH)/emsdk_env.sh &amp;&amp; $(MonoBuildTool) all install'" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoObjDir)" />
+  </Target>
+
   <!-- General targets -->
-  <Target Name="BuildMono" AfterTargets="Build" DependsOnTargets="BuildMonoRuntimeUnix;BuildMonoRuntimeWindows">
+  <Target Name="BuildMono" AfterTargets="Build" DependsOnTargets="BuildMonoRuntimeUnix;BuildMonoRuntimeWindows;BuildMonoRuntimeCMake">
     <PropertyGroup>
       <_MonoRuntimeFilePath Condition="'$(TargetsWindows)' == 'true' and '$(Platform)' == 'x64'">$(MonoObjDir)x64\Bin\$(Configuration)\mono-2.0-sgen.dll</_MonoRuntimeFilePath>
       <_MonoRuntimeFilePath Condition="'$(TargetsWindows)' == 'true' and '$(Platform)' == 'x86'">$(MonoObjDir)Win32\Bin\$(Configuration)\mono-2.0-sgen.dll</_MonoRuntimeFilePath>
-      <_MonoRuntimeFilePath Condition="'$(TargetsOSX)' == 'true'">$(MonoObjDir)mono\mini\.libs\libmonosgen-2.0.dylib</_MonoRuntimeFilePath>
+      <_MonoRuntimeFilePath Condition="'$(TargetsOSX)' == 'true' and '$(MonoEnableCMake)' != 'true'">$(MonoObjDir)mono\mini\.libs\libmonosgen-2.0.dylib</_MonoRuntimeFilePath>
+      <_MonoRuntimeFilePath Condition="'$(TargetsOSX)' == 'true' and '$(MonoEnableCMake)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.dylib</_MonoRuntimeFilePath>
       <_MonoRuntimeFilePath Condition="'$(TargetsiOS)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.dylib</_MonoRuntimeFilePath>
       <_MonoRuntimeFilePath Condition="'$(TargetstvOS)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.dylib</_MonoRuntimeFilePath>
       <_MonoRuntimeFilePath Condition="'$(TargetsAndroid)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.so</_MonoRuntimeFilePath>
-      <_MonoRuntimeFilePath Condition="'$(TargetsBrowser)' == 'true'">$(MonoObjDir)mono\mini\.libs\libmonosgen-2.0.a</_MonoRuntimeFilePath>
+      <_MonoRuntimeFilePath Condition="'$(TargetsBrowser)' == 'true' and '$(MonoEnableCMake)' != 'true'">$(MonoObjDir)mono\mini\.libs\libmonosgen-2.0.a</_MonoRuntimeFilePath>
+      <_MonoRuntimeFilePath Condition="'$(TargetsBrowser)' == 'true' and '$(MonoEnableCMake)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.a</_MonoRuntimeFilePath>
       <_MonoRuntimeFilePath Condition="'$(_MonoRuntimeFilePath)' == ''">$(MonoObjDir)mono\mini\.libs\libmonosgen-2.0.so</_MonoRuntimeFilePath>
       <_MonoRuntimeStaticFilePath Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsAndroid)' == 'true'">$(MonoObjDir)out\lib\libmonosgen-2.0.a</_MonoRuntimeStaticFilePath>
       <_MonoAotCrossFilePath Condition="'$(TargetsiOS)' == 'true' and '$(Platform)' == 'arm64'">$(MonoObjDir)cross\out\bin\aarch64-apple-darwin10-mono-sgen</_MonoAotCrossFilePath>
       <_MonoAotCrossFilePath Condition="'$(TargetsiOS)' == 'true' and '$(Platform)' == 'x64'">$(MonoObjDir)cross\out\bin\mono-sgen</_MonoAotCrossFilePath>
       <_MonoAotCrossFilePath Condition="'$(TargetstvOS)' == 'true' and '$(Platform)' == 'arm64'">$(MonoObjDir)cross\out\bin\aarch64-apple-darwin10-mono-sgen</_MonoAotCrossFilePath>
       <_MonoAotCrossFilePath Condition="'$(TargetstvOS)' == 'true' and '$(Platform)' == 'x64'">$(MonoObjDir)cross\out\bin\mono-sgen</_MonoAotCrossFilePath>
-      <_MonoAotCrossFilePath Condition="'$(TargetsBrowser)' == 'true'">$(MonoObjDir)cross\out\bin\wasm32-unknown-none-mono-sgen</_MonoAotCrossFilePath>
+      <_MonoAotCrossFilePath Condition="'$(TargetsBrowser)' == 'true' and '$(MonoEnableCMake)' != 'true'">$(MonoObjDir)cross\out\bin\wasm32-unknown-none-mono-sgen</_MonoAotCrossFilePath>
     </PropertyGroup>
 
     <!-- Copy Mono runtime files to artifacts directory -->
     <Exec Condition="'$(TargetsOSX)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'" Command="install_name_tool -id @rpath/$(MonoFileName) $(BinDir)$(MonoFileName)" />
   </Target>
 
+  <Target Name="CleanMono">
+    <RemoveDir Directories="$(MonoObjDir)" />
+  </Target>
+
 </Project>
index 69ab3a8..cf16461 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <config.h>
+#include <mono/utils/mono-compiler.h>
 
 #if !ENABLE_NETCORE
 
@@ -186,4 +187,8 @@ ves_icall_System_IO_KqueueMonitor_kevent_notimeout (int *kq_ptr, gpointer change
 
 #endif /* #if HAVE_KQUEUE */
 
+#else
+
+MONO_EMPTY_SOURCE_FILE (filewatcher);
+
 #endif /* !ENABLE_NETCORE */
index 67cc641..bee7f71 100644 (file)
@@ -913,4 +913,8 @@ mono_filesize_from_fd (int fd)
        return (gint64)buf.st_size;
 }
 
+#else
+
+MONO_EMPTY_SOURCE_FILE (w32file);
+
 #endif
index 0f148ae..eb73b89 100644 (file)
@@ -365,4 +365,8 @@ mono_w32process_platform_init_once (void)
 {
 }
 
+#else
+
+MONO_EMPTY_SOURCE_FILE (w32process_unix_default);
+
 #endif
index d3c38c5..8def599 100644 (file)
@@ -672,6 +672,57 @@ set(sgen_sources_base
 addprefix(sgen_sources ../sgen/ "${sgen_sources_base}")
 set_source_files_properties(${sgen_sources} PROPERTIES COMPILE_DEFINITIONS "HAVE_SGEN_GC")
 
+# EVENTPIPE
+
+set(eventpipe_sources_base
+       ep.c
+       ep.h
+       ep-block.c
+       ep-block.h
+       ep-buffer.c
+       ep-buffer.h
+       ep-buffer-manager.c
+       ep-buffer-manager.h
+       ep-config.c
+       ep-config.h
+       ep-config-internals.h
+       ep-event.c
+       ep-event.h
+       ep-event-instance.h
+       ep-event-instance.c
+       ep-event-payload.c
+       ep-event-payload.h
+       ep-event-source.c
+       ep-event-source.h
+       ep-file.c
+       ep-file.h
+       ep-getter-setter.h
+       ep-metadata-generator.c
+       ep-metadata-generator.h
+       ep-provider.c
+       ep-provider.h
+       ep-provider-internals.h
+       ep-rt.h
+       ep-rt-config.h
+       ep-rt-config-mono.h
+       ep-rt-mono.c
+       ep-rt-mono.h
+       ep-rt-types.h
+       ep-rt-types-mono.h
+       ep-thread.c
+       ep-thread.h
+       ep-types.h
+       ep-session.c
+       ep-session.h
+       ep-session-provider.c
+       ep-stack-contents.c
+       ep-stack-contents.h
+       ep-stream.c
+       ep-stream.h)
+if(ENABLE_PERFTRACING)
+  addprefix(eventpipe_sources ../eventpipe  "${eventpipe_sources_base}")
+endif()
+
 # ICU
 if(HAVE_SYS_ICU)
 if(STATIC_ICU)
@@ -879,7 +930,7 @@ addprefix(mini_public_headers ../mini "${mini_public_headers_base}")
 
 set(mini_sources "${CMAKE_CURRENT_BINARY_DIR}/buildver-sgen.h;main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
 
-add_library(monosgen-objects OBJECT "${eglib_sources};${metadata_sources};${utils_sources};${sgen_sources};${icu_shim_sources};${mini_sources}")
+add_library(monosgen-objects OBJECT "${eglib_sources};${metadata_sources};${utils_sources};${sgen_sources};${icu_shim_sources};${eventpipe_sources};${mini_sources}")
 add_library(monosgen-static STATIC $<TARGET_OBJECTS:monosgen-objects>)
 set_target_properties(monosgen-static PROPERTIES OUTPUT_NAME monosgen-2.0)
 install(TARGETS monosgen-static LIBRARY)
@@ -891,6 +942,7 @@ install(FILES ${mini_public_headers}
   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mono-2.0/mono/jit)
 if(NOT DISABLE_SHARED_LIBS)
    add_library(monosgen SHARED $<TARGET_OBJECTS:monosgen-objects>)
+   install(TARGETS monosgen LIBRARY)
 endif()
 
 if(NOT DISABLE_EXECUTABLES)
index 21cdbad..333d54d 100644 (file)
@@ -1,4 +1,5 @@
 #include <config.h>
+#include <mono/utils/mono-compiler.h>
 
 #if defined (HOST_WASM)
 
@@ -91,4 +92,8 @@ mono_dl_close_handle (MonoDl *module)
 {
 }
 
+#else
+
+MONO_EMPTY_SOURCE_FILE (mono_dl_wasm);
+
 #endif
index e0b20fe..33f4688 100644 (file)
@@ -2,7 +2,7 @@
 
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-mmap.h>
-
+#include <mono/utils/mono-compiler.h>
 
 #if defined (USE_WASM_BACKEND)
 
@@ -319,4 +319,8 @@ mono_memory_barrier_process_wide (void)
 {
 }
 
+#else
+
+MONO_EMPTY_SOURCE_FILE (mono_threads_wasm);
+
 #endif