Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active January 21, 2020 05:44
Show Gist options
  • Save kou1okada/7743f80e28b208245f0d8c4b96cb40ca to your computer and use it in GitHub Desktop.
Save kou1okada/7743f80e28b208245f0d8c4b96cb40ca to your computer and use it in GitHub Desktop.
hello.cs: A sample project for building C# with GNU Make.
project(hello LANGUAGES)
# enable_language(CSharp) # only suport VS2010 or later.
# add_executable(hello.cs)
if (CYGWIN)
set(MSBUILD /cygdrive/c/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe)
execute_process(COMMAND cygpath -a -w "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE DOS_STYLE_SOURCE_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
else (CYGWIN)
if (WIN32)
set(MSBUILD 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe')
else (WIN32)
set(MSBUILD xbuild)
endif(WIN32)
FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR) # Not works well on cygwin.
endif(CYGWIN)
CONFIGURE_FILE(hello.csproj.template hello.csproj)
include_external_msproject(hello.exe hello.csproj TYPE FAE04EC0-301F-11D3-BF4B-00C04F79EFBC) # for CMake 2.8.9 or later.
# But I wonder that below setting is effective or not.
add_custom_command(OUTPUT hello.exe MAIN_DEPENDENCY hello.cs COMMAND "${MSBUILD}")
add_custom_target(hello.exe ALL DEPENDS hello.exe)
#add_custom_target(hello.exe ALL COMMAND "${MSBUILD}") # This can't automatically append to clean files.
#set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES hello.exe) # Manually append to clean files.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project
DefaultTargets="hello"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="hello">
<!--
<ItemGroup>
<Compile Include="${DOS_STYLE_SOURCE_DIR}\**\*.cs" />
</ItemGroup>
-->
<!--
<ItemGroup>
<Compile Include="${DOS_STYLE_SOURCE_DIR}\*.cs" />
</ItemGroup>
-->
<Csc
Sources="${DOS_STYLE_SOURCE_DIR}\hello.cs"
OutputAssembly="hello.exe"
/>
</Target>
</Project>
include Makefile.cs.prepare
TARGETS = hello.exe
.PHONY: all clean
all: $(TARGETS)
hello.exe: hello.cs
$(CS) $<
clean:
-$(RM) $(TARGETS)
HOST = $(word 3,$(subst -, ,$(MAKE_HOST)))
ifneq ($(shell find -name Makefile.$(HOST)),)
include Makefile.$(HOST)
else
$(error Error: file not found: Makefile.$(HOST))
endif
ifneq ($(which "$(CS)"),)
$(error Error: command not found: $(CS))
endif
CS=/cygdrive/c/Windows/Microsoft.NET/Framework/v4.0.30319/csc.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment