Skip to content

Commit 5e77f6c

Browse files
authored
Improve skeleton generator (#59)
1 parent ab001fa commit 5e77f6c

File tree

3 files changed

+48
-61
lines changed

3 files changed

+48
-61
lines changed

source/MetadataProcessor.Core/SkeletonGenerator/AssemblyClassTable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AssemblyClassTable
1212
public string AssemblyName;
1313
public string HeaderFileName;
1414
public string ProjectName;
15+
public bool IsInterop;
1516

1617
public List<Class> Classes = new List<Class>();
1718
}

source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ struct Library_{{AssemblyName}}_{{Name}}{{#newline}}
9999
};{{#newline}}
100100
{{#newline}}
101101
102-
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{AssemblyName}} ={{#newline}}
102+
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{Name}} ={{#newline}}
103103
{{{#newline}}
104-
""{{Name}}"",{{#newline}}
104+
""{{AssemblyName}}"",{{#newline}}
105105
{{NativeCRC32}},{{#newline}}
106106
method_lookup,{{#newline}}
107107
{ {{NativeVersion.Major}}, {{NativeVersion.Minor}}, {{NativeVersion.Build}}, {{NativeVersion.Revision}} }{{#newline}}
@@ -238,34 +238,42 @@ struct {{ClassName}}
238238

239239
internal const string CMakeModuleTemplate =
240240
@"#
241-
# Copyright(c) 2018 The nanoFramework project contributors
241+
# Copyright(c) 2020 The nanoFramework project contributors
242242
# See LICENSE file in the project root for full license information.
243243
#
244244
245-
#################################################################################################
246-
# make sure that a valid path is set bellow #
247-
# if this is for a class library it's OK to leave it as it is #
248-
# if this is an Interop module the path has to be set where the build can find the source files #
249-
#################################################################################################
245+
{{#if IsInterop}}########################################################################################
246+
# make sure that a valid path is set bellow #
247+
# this is an Interop module so this file should be placed in the CMakes module folder #
248+
# usually CMake\Modules #
249+
########################################################################################
250250
251251
# native code directory
252+
set(BASE_PATH_FOR_THIS_MODULE ${PROJECT_SOURCE_DIR}/InteropAssemblies/{{AssemblyName}})
253+
{{#else}}# native code directory
252254
set(BASE_PATH_FOR_THIS_MODULE ${BASE_PATH_FOR_CLASS_LIBRARIES_MODULES}/{{AssemblyName}})
253-
255+
{{/if}}
254256
255257
# set include directories
256258
list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/CLR/Core)
257259
list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/CLR/Include)
258260
list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/HAL/Include)
259261
list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Include)
260262
list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${BASE_PATH_FOR_THIS_MODULE})
263+
{{#if IsInterop}}{{#else}}list(APPEND {{AssemblyName}}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/{{AssemblyName}}){{/if}}
261264
262265
# source files
263266
set({{AssemblyName}}_SRCS
264-
{{HeaderFileName}}.cpp
267+
268+
{{ProjectName}}.cpp
269+
{{#if IsInterop}}
265270
{{#each Classes}}
266271
{{HeaderFileName}}_{{Name}}_mshl.cpp
267272
{{HeaderFileName}}_{{Name}}.cpp{{/each}}
268-
273+
{{#else}}
274+
{{#each Classes}}
275+
{{HeaderFileName}}_{{Name}}.cpp{{/each}}
276+
{{/if}}
269277
)
270278
271279
foreach(SRC_FILE ${{{AssemblyName}}_SRCS})

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ private void GenerateStubs()
6464
var classList = new AssemblyClassTable
6565
{
6666
AssemblyName = _tablesContext.AssemblyDefinition.Name.Name,
67-
ProjectName = _safeProjectName
67+
ProjectName = _safeProjectName,
68+
IsInterop = !_withoutInteropCode
6869
};
6970

7071
foreach (var c in _tablesContext.TypeDefinitionTable.TypeDefinitions)
@@ -81,22 +82,10 @@ private void GenerateStubs()
8182
ClassName = c.Name,
8283
ShortNameUpper = $"{_assemblyName}_{_safeProjectName}_{className}".ToUpper(),
8384
RootNamespace = _assemblyName,
84-
ProjectName = _safeProjectName
85+
ProjectName = _safeProjectName,
86+
HeaderFileName = _safeProjectName
8587
};
8688

87-
if (!_withoutInteropCode)
88-
{
89-
// Interop code needs to use the root namespace
90-
91-
classStubs.HeaderFileName = $"{_assemblyName}_{_safeProjectName}";
92-
}
93-
else
94-
{
95-
// projects with Interop can use a simplified naming
96-
97-
classStubs.HeaderFileName = _safeProjectName;
98-
}
99-
10089
classList.Classes.Add(new Class()
10190
{
10291
Name = className
@@ -241,7 +230,7 @@ private void GenerateStubs()
241230
// user code stub
242231
Generator generator = compiler.Compile(SkeletonTemplates.ClassStubTemplate);
243232

244-
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_assemblyName}_{_safeProjectName}_{className}.cpp")))
233+
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_safeProjectName}_{className}.cpp")))
245234
{
246235
var output = generator.Render(classStubs);
247236
headerFile.Write(output);
@@ -250,7 +239,7 @@ private void GenerateStubs()
250239
// marshal code
251240
generator = compiler.Compile(SkeletonTemplates.ClassMarshallingCodeTemplate);
252241

253-
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_assemblyName}_{_safeProjectName}_{className}_mshl.cpp")))
242+
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_safeProjectName}_{className}_mshl.cpp")))
254243
{
255244
var output = generator.Render(classStubs);
256245
headerFile.Write(output);
@@ -259,7 +248,7 @@ private void GenerateStubs()
259248
// class header
260249
generator = compiler.Compile(SkeletonTemplates.ClassHeaderTemplate);
261250

262-
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_assemblyName}_{_safeProjectName}_{className}.h")))
251+
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_safeProjectName}_{className}.h")))
263252
{
264253
var output = generator.Render(classStubs);
265254
headerFile.Write(output);
@@ -269,8 +258,7 @@ private void GenerateStubs()
269258
}
270259
}
271260

272-
if (!_withoutInteropCode &&
273-
classList.Classes.Count > 0)
261+
if (classList.Classes.Count > 0)
274262
{
275263
FormatCompiler compiler = new FormatCompiler
276264
{
@@ -280,8 +268,20 @@ private void GenerateStubs()
280268
// CMake module
281269
Generator generator = compiler.Compile(SkeletonTemplates.CMakeModuleTemplate);
282270

283-
// FindINTEROP-NF.AwesomeLib
284-
using (var headerFile = File.CreateText(Path.Combine(_path, $"FindINTEROP-{_safeProjectName}.cmake")))
271+
string fileName;
272+
273+
if (!_withoutInteropCode)
274+
{
275+
// this is an Interop library: FindINTEROP-NF.AwesomeLib.cmake
276+
fileName = Path.Combine(_path, $"FindINTEROP-{classList.AssemblyName}.cmake");
277+
}
278+
else
279+
{
280+
// this is a class library: FindWindows.Devices.Gpio.cmake
281+
fileName = Path.Combine(_path, $"Find{classList.AssemblyName}.cmake");
282+
}
283+
284+
using (var headerFile = File.CreateText(fileName))
285285
{
286286
var output = generator.Render(classList);
287287
headerFile.Write(output);
@@ -298,8 +298,8 @@ private void GenerateAssemblyLookup()
298298
var assemblyLookup = new AssemblyLookupTable()
299299
{
300300
IsCoreLib = _isCoreLib,
301-
Name = _name,
302-
AssemblyName = _assemblyName,
301+
Name = _assemblyName,
302+
AssemblyName = _tablesContext.AssemblyDefinition.Name.Name,
303303
HeaderFileName = _safeProjectName,
304304
NativeVersion = nativeVersion,
305305
NativeCRC32 = "0x" + _tablesContext.NativeMethodsCrc.Current.ToString("X")
@@ -373,20 +373,8 @@ private void GenerateAssemblyLookup()
373373

374374
FormatCompiler compiler = new FormatCompiler();
375375
Generator generator = compiler.Compile(SkeletonTemplates.AssemblyLookupTemplate);
376-
string filePath;
377-
378-
if (!_withoutInteropCode)
379-
{
380-
// Interop code needs to use the root namespace
381-
filePath = Path.Combine(_path, $"{_assemblyName}_{_safeProjectName}.cpp");
382-
}
383-
else
384-
{
385-
// projects with Interop can use a simplified naming
386-
filePath = Path.Combine(_path, $"{_safeProjectName}.cpp");
387-
}
388376

389-
using (var headerFile = File.CreateText(filePath))
377+
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_safeProjectName}.cpp")))
390378
{
391379
var output = generator.Render(assemblyLookup);
392380
headerFile.Write(output);
@@ -505,21 +493,11 @@ private void GenerateAssemblyHeader()
505493
FormatCompiler compiler = new FormatCompiler();
506494
Generator generator = compiler.Compile(SkeletonTemplates.AssemblyHeaderTemplate);
507495

496+
// create stubs directory
508497
Directory.CreateDirectory(_path);
509-
string filePath;
510-
511-
if (!_withoutInteropCode)
512-
{
513-
// Interop code needs to use the root namespace
514-
filePath = Path.Combine(_path, $"{_assemblyName}_{_safeProjectName}.h");
515-
}
516-
else
517-
{
518-
// projects with Interop can use a simplified naming
519-
filePath = Path.Combine(_path, $"{_safeProjectName}.h");
520-
}
521498

522-
using (var headerFile = File.CreateText(filePath))
499+
// output header file
500+
using (var headerFile = File.CreateText(Path.Combine(_path, $"{_safeProjectName}.h")))
523501
{
524502
var output = generator.Render(assemblyData);
525503
headerFile.Write(output);

0 commit comments

Comments
 (0)