Skip to content

Commit a482210

Browse files
committed
Support building with msbuild
MSBuild is open source -- has been for ages -- and we'd *really* like to migrate all things Mono to MSBuild and deprecate/remove `xbuild`. Which means we need to ensure that all of our products build with `msbuild`. [Which unfortunately isn't the case in `Java.Interop`][0]: MSB3375: The file "../../bin/Release/Xamarin.Android.Cecil.dll" does not exist. ... Java.Interop/MarshalMemberBuilder.cs(9,20): error CS0234: The type or namespace name `Expressions' does not exist in the namespace `Java.Interop'. Are you missing an assembly reference? ... 36 Warning(s) 69 Error(s) For starters, bring Java.Interop into aligntment/convention with the [`xamarin-android` repo][1], so that `$(MSBUILD)` is the Make variable to specify the MSBuild engine to use, not `$(XBUILD)`, and allow `$(V)` to set `$MONO_OPTIONS` so that line numbers are included in stack traces from mono. Which brings us to the build errors. The MSB3375 error happens when building e.g. `Xamarin.Android.Cecil.csproj` from another directory: $ msbuild src/Xamarin.Android.Cecil/Xamarin.Android.Cecil.csproj ... .../src/Xamarin.Android.Cecil/Xamarin.Android.Cecil.targets(33,5): error MSB3375: The file "../../bin/Debug/Xamarin.Android.Cecil.dll" does not exist. ... The cause, in turn, is because the nested `<MSBuild/>` invocation overrides `$(OutputPath)` when building `external/Cecil/Mono.Cecil.csproj` to be *`Xamarin.Android.Cecil.csproj`'s* `$(OutputPath)`: <PropertyGroup> <CecilOutputPath>$([System.IO.Path]::GetFullPath ('$(OutputPath)'))</CecilOutputPath> </PropertyGroup> <MSBuild Properties="OutputPath=$(CecilOutputPath)" ... /> Or rather, it *tries* to. It uses `Path.GetFullPath()` on `$(OutputPath)` so that `Mono.Cecil.csproj` writes the assembly into the expected `Xamarin.Android.Cecil.csproj`-specified directory. The problem is that the `Path.GetFullPath()` call is relative to the *current working directory*, so when: 1. The current working directory isn't the same directory as the directory containing `Xamarin.Android.Cecil.csproj`, and 2. We're building `Xamarin.Android.Cecil.csproj`, which defines `$(OutputPath)` as e.g. `..\..\Debug` (1) and (2) interplay so that `$(CecilOutputPath)` becomes e.g. `$CWD/../../bin/Debug`, which is *not* the correct directory, and may (will!) be outside of the repo checkout directory. Modify the `$(CecilOutputPath)` definition so that it instead assumes that `$(OutputPath)` is a directory *relative to* `$(MSBuildThisFileDirectory)`, equivalent to C#: var OutputPath = ... var MSBuildThisFileDirectory = ... var CecilOutputPath = Path.Combine (MSBuildThisFileDirectory, OutputPath); CecilOutputPath = Path.GetFullPath (CecilOutputPath); This fixes the above interplay by "inserting" use of `$(MSBuildThisFileDirectory)` into the "normal" `msbuild Xamarin.Android.Cecil.csproj` invocation path, ensuring that output paths behave as expected. This in turn allows `Xamarin.Android.Cecil.dll` to be placed into the correct directory, which in turn fixes all of the other C# errors (present apparently because `Xamarin.Android.Cecil.dll` couldn't be resolved). [0]: https://jenkins.mono-project.com/view/Xamarin.Android/job/Java.Interop-msbuild/5/consoleText [1]: https://github.com/xamarin/xamarin-android
1 parent 769f944 commit a482210

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

Makefile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
OS ?= $(shell uname)
22

3+
V ?= 0
34
CONFIGURATION = Debug
45

56
ifeq ($(OS),Darwin)
@@ -39,7 +40,6 @@ PTESTS = \
3940
ATESTS = \
4041
bin/Test$(CONFIGURATION)/Android.Interop-Tests.dll
4142

42-
XBUILD = xbuild $(if $(V),/v:diag,)
4343
NUNIT_CONSOLE = packages/NUnit.Runners.2.6.3/tools/nunit-console.exe
4444

4545
BUILD_PROPS = bin/Build$(CONFIGURATION)/JdkInfo.props bin/Build$(CONFIGURATION)/MonoInfo.props
@@ -57,12 +57,13 @@ prepare-external: $(PACKAGES) $(NUNIT_CONSOLE)
5757
git submodule update --init --recursive
5858

5959
clean:
60-
-$(XBUILD) /t:Clean
60+
-$(MSBUILD) $(MSBUILD_FLAGS) /t:Clean
6161
-rm -Rf bin/$(CONFIGURATION) bin/Build$(CONFIGURATION) bin/Test$(CONFIGURATION) bin/XAIntegration$(CONFIGURATION)
6262
-rm src/Java.Runtime.Environment/Java.Runtime.Environment.dll.config
6363

6464
include build-tools/scripts/mono.mk
6565
include build-tools/scripts/jdk.mk
66+
include build-tools/scripts/msbuild.mk
6667

6768
$(PACKAGES) $(NUNIT_CONSOLE):
6869
nuget restore
@@ -97,7 +98,7 @@ endif
9798
# Usage: $(call TestAssemblyTemplate,assembly-basename)
9899
define TestAssemblyTemplate
99100
bin/Test$$(CONFIGURATION)/$(1)-Tests.dll: $(wildcard src/$(1)/*/*.cs src/$(1)/Test*/*/*.cs)
100-
$$(XBUILD)
101+
$$(MSBUILD) $$(MSBUILD_FLAGS)
101102
touch $$@
102103
endef # TestAssemblyTemplate
103104

@@ -107,15 +108,15 @@ $(eval $(call TestAssemblyTemplate,Java.Interop.Export))
107108
$(eval $(call TestAssemblyTemplate,Java.Interop.Tools.JavaCallableWrappers))
108109

109110
bin/Test$(CONFIGURATION)/Java.Interop-PerformanceTests.dll: $(wildcard tests/Java.Interop-PerformanceTests/*.cs) bin/Test$(CONFIGURATION)/$(NATIVE_TIMING_LIB)
110-
$(XBUILD)
111+
$(MSBUILD) $(MSBUILD_FLAGS)
111112
touch $@
112113

113114
bin/Test$(CONFIGURATION)/Android.Interop-Tests.dll: $(wildcard src/Android.Interop/*/*.cs src/Android.Interop/Tests/*/*.cs)
114-
$(XBUILD)
115+
$(MSBUILD) $(MSBUILD_FLAGS)
115116
touch $@
116117

117118
bin/$(XA_CONFIGURATION)/Java.Interop.dll: $(wildcard src/Java.Interop/*/*.cs) src/Java.Interop/Java.Interop.csproj
118-
$(XBUILD) /p:Configuration=$(XA_CONFIGURATION) $(if $(SNK),"/p:AssemblyOriginatorKeyFile=$(SNK)",)
119+
$(MSBUILD) $(if $(V),/v:diag,) /p:Configuration=$(XA_CONFIGURATION) $(if $(SNK),"/p:AssemblyOriginatorKeyFile=$(SNK)",)
119120

120121
CSHARP_REFS = \
121122
bin/$(CONFIGURATION)/Java.Interop.dll \
@@ -150,7 +151,7 @@ bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB): bin/$(CONFIGURATION)/$(JAVA_INTERO
150151
cp $< $@
151152

152153
run-android: $(ATESTS)
153-
(cd src/Android.Interop/Tests; $(XBUILD) '/t:Install;RunTests' $(if $(FIXTURE),/p:TestFixture=$(FIXTURE)))
154+
(cd src/Android.Interop/Tests; $(MSBUILD) $(MSBUILD_FLAGS) '/t:Install;RunTests' $(if $(FIXTURE),/p:TestFixture=$(FIXTURE)))
154155

155156
run-test-jnimarshal: bin/Test$(CONFIGURATION)/Java.Interop.Export-Tests.dll bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB)
156157
MONO_TRACE_LISTENER=Console.Out \

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ on the command line or by specifying MSBuild properties to control behavior.
7676
The following **make**(1) variables may be specified:
7777

7878
* `$(CONFIGURATION)`: The product configuration to build, and corresponds
79-
to the `$(Configuration)` MSBuild property when running `$(XBUILD)`.
79+
to the `$(Configuration)` MSBuild property when running `$(MSBUILD)`.
8080
Valid values are `Debug` and `Release`. Default value is `Debug`.
8181
* `$(RUNTIME)`: The managed runtime to use to execute utilities, tests.
8282
Default value is `mono64` if present in `$PATH`, otherwise `mono`.
@@ -85,9 +85,9 @@ The following **make**(1) variables may be specified:
8585

8686
make run-tests TESTS=bin/Debug/Java.Interop.Dynamic-Tests.dll
8787

88-
* `$(V)`: If set to a non-empty string, adds `/v:diag` to `$(XBUILD)`
88+
* `$(V)`: If set to a non-empty string, adds `/v:diag` to `$(MSBUILD_FLAGS)`
8989
invocations.
90-
* `$(XBUILD)`: The MSBuild build tool to execute for builds.
90+
* `$(MSBUILD)`: The MSBuild build tool to execute for builds.
9191
Default value is `xbuild`.
9292

9393

build-tools/scripts/mono.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66
# $(OS): Optional; **uname**(1) value of the host operating system
77
# $(CONFIGURATION): Build configuration name, e.g. Debug or Release
8+
# $(V): Output verbosity. If != 0, then `MONO_OPTIONS` is exported with --debug.
89
#
910
# Outputs:
1011
#
@@ -27,6 +28,14 @@
2728
OS ?= $(shell uname)
2829
RUNTIME := $(shell if [ -f "`which mono64`" ] ; then echo mono64 ; else echo mono; fi) --debug=casts
2930

31+
ifneq ($(V),0)
32+
MONO_OPTIONS += --debug
33+
endif # $(V) != 0
34+
35+
ifneq ($(MONO_OPTIONS),)
36+
export MONO_OPTIONS
37+
endif # $(MONO_OPTIONS) != ''
38+
3039
ifeq ($(OS),Darwin)
3140
JI_MONO_FRAMEWORK_PATH = /Library/Frameworks/Mono.framework/Libraries/libmonosgen-2.0.1.dylib
3241
JI_MONO_INCLUDE_PATHS = /Library/Frameworks/Mono.framework/Headers/mono-2.0

src/Xamarin.Android.Cecil/Xamarin.Android.Cecil.targets

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
<PropertyGroup>
44
<CecilDirectory>$(MSBuildThisFileDirectory)\..\..\external\cecil</CecilDirectory>
55
<CecilPreparedFlag>prepared.flag</CecilPreparedFlag>
6-
<OutputPath Condition=" '$(OutputPath)' == '' ">$(MSBuildThisFileDirectory)\..\..\bin\$(Configuration)</OutputPath>
7-
<CecilOutputPath>$([System.IO.Path]::GetFullPath ('$(OutputPath)'))</CecilOutputPath>
6+
<OutputPath Condition=" '$(OutputPath)' == '' ">..\..\bin\$(Configuration)</OutputPath>
7+
<CecilOutputPath>$([System.IO.Path]::Combine ($(MSBuildThisFileDirectory), $(OutputPath)))</CecilOutputPath>
8+
<CecilOutputPath>$([System.IO.Path]::GetFullPath ($(CecilOutputPath)))</CecilOutputPath>
89
<CecilAssemblies>$(OutputPath)\Xamarin.Android.Cecil.dll;$(OutputPath)\Xamarin.Android.Cecil.Mdb.dll</CecilAssemblies>
910
</PropertyGroup>
1011
<Target Name="PrepareCecil"

src/java-interop/java-interop.mdproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
Inputs="@(Compile)"
8282
Outputs="@(MacLibLipo)">
8383
<PropertyGroup>
84-
<_FixedDefines>$(DefineSymbols.Replace(' ', ';'))</_FixedDefines>
84+
<_FixedDefines>$(DefineSymbols.Split(' '))</_FixedDefines>
8585
</PropertyGroup>
8686
<ItemGroup>
8787
<_Defines Include="$(_FixedDefines)" />

tests/invocation-overhead/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ clean:
99

1010
include ../../build-tools/scripts/mono.mk
1111
include ../../build-tools/scripts/jdk.mk
12+
include ../../build-tools/scripts/msbuild.mk
1213

1314
$(JNIENV_GEN):
14-
(cd ../../build-tools/jnienv-gen ; xbuild /p:Configuration=$(CONFIGURATION))
15+
(cd ../../build-tools/jnienv-gen ; $(MSBUILD) $(MSBUILD_FLAGS) )
1516

1617
HANDLE_FEATURES = \
1718
-d:FEATURE_JNIENVIRONMENT_JI_INTPTRS \

0 commit comments

Comments
 (0)