Skip to content

Commit

Permalink
mapscript_csharp compilable on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
R committed Jan 15, 2019
1 parent 418e15c commit f46ecbb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
43 changes: 39 additions & 4 deletions mapscript/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PROGRAM (CSHARP_COMPILER NAMES csc gmcs gmcs2)
FIND_PROGRAM (CSHARP_COMPILER NAMES csc mcs gmcs gmcs2)

IF (CSHARP_COMPILER)
MESSAGE(STATUS "Found CSharp compiler: ${CSHARP_COMPILER}")
Expand All @@ -25,6 +25,8 @@ if (WIN32)
if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
set(PLATFORM_TARGET ${PLATFORM_TARGET} /debug:full)
endif (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
else()
set (KEYFILE_SPEC -keyfile:${PROJECT_SOURCE_DIR}/mapscript/csharp/mapscript.snk)
endif(WIN32)

MARK_AS_ADVANCED(CSHARP_COMPILER)
Expand All @@ -33,15 +35,24 @@ include_directories(${PROJECT_SOURCE_DIR}/mapscript/swiginc)
include_directories(${PROJECT_SOURCE_DIR}/mapscript/)
include_directories(${PROJECT_SOURCE_DIR}/mapscript/csharp)
SET (CMAKE_SWIG_OUTDIR "${CMAKE_CURRENT_BINARY_DIR}")
SET( CMAKE_SWIG_FLAGS -namespace OSGeo.MapServer ${MAPSERVER_COMPILE_DEFINITIONS} -DWIN32)
SWIG_ADD_MODULE(mapscript csharp ../mapscript.i)
SET( CMAKE_SWIG_FLAGS -namespace OSGeo.MapServer ${MAPSERVER_COMPILE_DEFINITIONS})
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
SWIG_ADD_MODULE(mapscript csharp ../mapscript.i)
else()
SWIG_ADD_LIBRARY(mapscript LANGUAGE csharp SOURCES ../mapscript.i)
endif()


set_target_properties(mapscript PROPERTIES OUTPUT_NAME "mapscript")

SWIG_LINK_LIBRARIES(mapscript ${MAPSERVER_LIBMAPSERVER})


ADD_CUSTOM_COMMAND(TARGET mapscript


if (WIN32)

ADD_CUSTOM_COMMAND(TARGET mapscript
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
POST_BUILD
COMMAND set MAPSCRIPT_SNK=${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\mapscript.snk
Expand All @@ -61,5 +72,29 @@ ADD_CUSTOM_COMMAND(TARGET mapscript
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF /r:mapscript_csharp.dll /r:System.Xaml.dll /r:WindowsBase.dll /r:PresentationCore.dll /out:drawmapWPF.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmapWPF.cs
COMMENT "Compiling c# source files"
)
else()
ADD_CUSTOM_COMMAND(TARGET mapscript
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
POST_BUILD
#COMMAND set MAPSCRIPT_SNK=${PROJECT_SOURCE_DIR}/mapscript/csharp/mapscript.snk
#COMMAND set MAPSCRIPT_SNK=%MAPSCRIPT_SNK:/=\\%
COMMAND cp -f ${PROJECT_SOURCE_DIR}/mapscript/csharp/mapscript.snk ./
COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} -target:library -out:mapscript_csharp.dll ${KEYFILE_SPEC} *.cs ${PROJECT_SOURCE_DIR}/mapscript/csharp/config/AssemblyInfo.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:shpdump.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\shpdump.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:drawmap.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmap.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:shapeinfo.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\shapeinfo.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:drawquery.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawquery.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /r:System.Drawing.dll /out:getbytes.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\getbytes.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:HTMLtemplate.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\HTMLtemplate.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /out:RFC24.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\RFC24.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /r:System.Drawing.dll /out:drawmapDirect.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmapDirect.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /r:System.Drawing.dll /out:drawmapDirectPrint.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmapDirectPrint.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /r:mapscript_csharp.dll /r:System.Drawing.dll /out:drawmapGDIPlus.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmapGDIPlus.cs
#COMMAND ${CSHARP_COMPILER} ${PLATFORM_TARGET} /lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF /r:mapscript_csharp.dll /r:System.Xaml.dll /r:WindowsBase.dll /r:PresentationCore.dll /out:drawmapWPF.exe ${PROJECT_SOURCE_DIR}\\mapscript\\csharp\\examples\\drawmapWPF.cs
COMMENT "Compiling c# source files"
)
endif()

#get_target_property(LOC_MAPSCRIPT_LIB ${SWIG_MODULE_csharpmapscript_REAL_NAME} LOCATION)
#install(FILES ${LOC_MAPSCRIPT_LIB} DESTINATION lib)

18 changes: 9 additions & 9 deletions mapscript/csharp/csmodule.i
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ inner exceptions. Otherwise the exception message will be concatenated*/
*****************************************************************************/

%pragma(csharp) imclasscode=%{
public class StringArrayMarshal : System.IDisposable {
public class StringArrayMarshal : global::System.IDisposable {
public readonly System.IntPtr[] _ar;
public StringArrayMarshal(string[] ar) {
_ar = new System.IntPtr[ar.Length];
Expand Down Expand Up @@ -208,8 +208,8 @@ inner exceptions. Otherwise the exception message will be concatenated*/

%typemap(csvarout, excode=SWIGEXCODE2) outputFormatObj** %{
get {
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
outputFormatObj[] ret = new outputFormatObj[this.numoutputformats];
for(int cx = 0; cx < this.numoutputformats; cx++) {
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)));
Expand Down Expand Up @@ -313,7 +313,7 @@ static SWIG_CSharpByteArrayHelperCallback SWIG_csharp_bytearray_callback = NULL;
%csmethodmodifiers processLegendTemplate "private";
%csmethodmodifiers processQueryTemplate "private";

%typemap(csinterfaces) mapObj "System.IDisposable, System.Runtime.Serialization.ISerializable";
%typemap(csinterfaces) mapObj "System.IDisposable, System.Runtime.Serialization.ISerializable";
%typemap(csattributes) mapObj "[Serializable]"
%typemap(cscode) mapObj, struct mapObj %{
public string processTemplate(int bGenerateImages, string[] names, string[] values)
Expand Down Expand Up @@ -393,7 +393,7 @@ static SWIG_CSharpByteArrayHelperCallback SWIG_csharp_bytearray_callback = NULL;
}
}
protected static SWIGByteArrayHelper bytearrayHelper = new SWIGByteArrayHelper();
[System.ThreadStatic]
[System.ThreadStatic]
private static byte[] arraybuffer;

internal static byte[] GetBytes()
Expand Down Expand Up @@ -424,7 +424,7 @@ DllExport void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByte
%typemap(csin) (double pattern[ANY]) "$csinput"
%typemap(csvarout, excode=SWIGEXCODE2) (double pattern[ANY]) %{
get {
System.IntPtr cPtr = $imcall;
System.IntPtr cPtr = $imcall;
double[] ret = new double[patternlength];
if (patternlength > 0) {
System.Runtime.InteropServices.Marshal.Copy(cPtr, ret, 0, patternlength);
Expand All @@ -433,7 +433,7 @@ DllExport void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByte
return ret;
}
set {
System.IntPtr cPtr = $imcall;
System.IntPtr cPtr = $imcall;
if (value.Length > 0) {
System.Runtime.InteropServices.Marshal.Copy(value, 0, cPtr, value.Length);
}
Expand All @@ -450,8 +450,8 @@ DllExport void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByte
%typemap(csin) (int *panIndexes) "$csinput"

/* Typemaps for device handle */
%typemap(imtype) (void* device) %{ System.IntPtr%}
%typemap(cstype) (void* device) %{ System.IntPtr%}
%typemap(imtype) (void* device) %{System.IntPtr%}
%typemap(cstype) (void* device) %{System.IntPtr%}
%typemap(in) (void* device) %{ $1 = ($1_ltype)$input; %}
%typemap(csin) (void* device) "$csinput"

Expand Down
14 changes: 6 additions & 8 deletions mapscript/csharp/swig_csharp_extensions.i
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

// Ensure the class is not marked BeforeFieldInit causing memory corruption with CLR4
%pragma(csharp) imclasscode=%{
//static $imclassname() {
//}

public class UTF8Marshaler : System.Runtime.InteropServices.ICustomMarshaler {
static UTF8Marshaler static_instance;
Expand All @@ -45,12 +43,12 @@
"UTF8Marshaler must be used on a string.");

// not null terminated
byte[] strbuf = System.Text.Encoding.UTF8.GetBytes((string)managedObj);
byte[] strbuf = System.Text.Encoding.UTF8.GetBytes((string)managedObj);
System.IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(strbuf.Length + 1);
System.Runtime.InteropServices.Marshal.Copy(strbuf, 0, buffer, strbuf.Length);

// write the terminating null
System.Runtime.InteropServices.Marshal.WriteByte(buffer, strbuf.Length, 0);
System.Runtime.InteropServices.Marshal.WriteByte(buffer, strbuf.Length, 0);
return buffer;
}

Expand All @@ -62,7 +60,7 @@
}

public void CleanUpNativeData(System.IntPtr pNativeData) {
System.Runtime.InteropServices.Marshal.FreeHGlobal(pNativeData);
System.Runtime.InteropServices.Marshal.FreeHGlobal(pNativeData);
}

public void CleanUpManagedData(object managedObj) {
Expand All @@ -81,8 +79,8 @@
}
%}

%typemap(imtype, inattributes="[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]",
outattributes="[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]")
%typemap(imtype, inattributes="[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]",
outattributes="[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]")
char *, char *&, char[ANY], char[] "string"

%typemap(csout, excode=SWIGEXCODE) SWIGTYPE {
Expand Down Expand Up @@ -267,7 +265,7 @@

%pragma(csharp) modulecode=%{
/* %pragma(csharp) modulecode */
internal class $moduleObject : System.IDisposable {
internal class $moduleObject : global::System.IDisposable {
public virtual void Dispose() {

}
Expand Down

0 comments on commit f46ecbb

Please sign in to comment.