Skip to content

Commit

Permalink
NASM basic support
Browse files Browse the repository at this point in the history
  • Loading branch information
PotvinAUbi committed Nov 7, 2024
1 parent d66657c commit 52078f8
Show file tree
Hide file tree
Showing 26 changed files with 731 additions and 17 deletions.
16 changes: 15 additions & 1 deletion SamplesDef.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,20 @@
"./RunSharpmake.ps1 -workingDirectory {testFolder} -sharpmakeFile \"HelloIOS.Main.sharpmake.cs\" -framework {framework}",
"xcodebuild build-for-testing CODE_SIGNING_ALLOWED=NO -workspace {testFolder}/codebase/temp/solutions/HelloIOS_iOS.xcworkspace -configuration {configuration} -scheme exe_iOS -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 14 Pro' test -derivedDataPath {testFolder}/test"
]
}
},
{
"Name": "HelloAssemblyNasm",
"CIs": [ "github", "gitlab" ],
"OSs": [ "windows-2022" ],
"Frameworks": [ "net6.0" ],
"Configurations": [ "debug", "release" ],
"TestFolder": "samples/HelloAssemblyNasm",
"Commands":
[
"./RunSharpmake.ps1 -workingDirectory {testFolder} -sharpmakeFile \"HelloAssemblyNasm.sharpmake.cs\" -framework {framework}",
"./Compile.ps1 -slnOrPrjFile \"helloassemblynasm_{VsVersionSuffix}_win64_msbuild.sln\" -configuration {configuration} -platform \"x64\" -WorkingDirectory \"{testFolder}/projects\" -VsVersion {os} -compiler MsBuild",
"&'./{testFolder}/projects/output/win64/{configuration}/HelloAssemblyNasmExecutable.exe'"
]
},
]
}
1 change: 1 addition & 0 deletions Sharpmake.Generators/CompilerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Configuration
public string Linker { get; set; }
public string PlatformLibPaths { get; set; }
public string Masm { get; set; }
public string Nasm { get; set; }
public string Executable { get; set; }
public string UsingOtherConfiguration { get; set; }
public Platform Platform { get; private set; }
Expand Down
26 changes: 26 additions & 0 deletions Sharpmake.Generators/FastBuild/Bff.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static class ConfigurationFile
";

public static string MasmConfigNameSuffix = "Masm";
public static string NasmConfigNameSuffix = "Nasm";
public static string Win64ConfigName = ".win64Config";

public static string CompilerSetting = @"
Expand Down Expand Up @@ -120,6 +121,15 @@ public static class ConfigurationFile
.Executable = '[fastBuildMasmCompiler]'
.CompilerFamily = 'custom'
}
";

// TODOANT
internal static string NasmCompilerSettings = @"
Compiler( '[fastBuildNasmCompilerName]' )
{
.Executable = '[fastBuildNasmCompiler]'
.CompilerFamily = 'custom'
}
";

public static string CompilerConfiguration = @"
Expand Down Expand Up @@ -296,6 +306,14 @@ public static class ConfigurationFile
// ----------------
.CompilerOptions = ' $CompilerExtraOptions$'
+ ' /Fo""%2"" /c /Ta ""%1""'
";
// TODOANT
public static string CompilerOptionsNasm = @"
// Compiler options
// ----------------
.CompilerOptions = ' $CompilerExtraOptions$'
+ ' -Xvc -Ox -o""%2"" ""%1""'
+ ' [cmdLineOptions.NasmCompilerFormat] '
";
public static string CompilerOptionsClang = @"
// Compiler options
Expand Down Expand Up @@ -334,6 +352,14 @@ public static class ConfigurationFile
+ ' [cmdLineOptions.PreprocessorDefinitions]'
";

// TODOANT: NasmCompilerExtraOptions
public static string NasmCompilerExtraOptions = @"
.CompilerExtraOptions = ''
+ ' [cmdLineOptions.AdditionalAssemblyNasmIncludeDirectories]'
+ ' [cmdLineOptions.NasmPreprocessorDefinitions]'
+ ' [cmdLineOptions.PreIncludedFiles]'
";

public static string CPPCompilerExtraOptions = @"
.CompilerExtraOptions = ''
+ ' [cmdLineOptions.AdditionalIncludeDirectories]'
Expand Down
108 changes: 97 additions & 11 deletions Sharpmake.Generators/FastBuild/Bff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private enum Languages
CPP = 4,
ObjC = 8,
ObjCPP = 16,
Swift = 32
Swift = 32,
Nasm = 64
}

[Flags]
Expand Down Expand Up @@ -357,6 +358,7 @@ List<string> skipFiles
bool isCompileAsObjCPPFile = subConfig.Languages.HasFlag(Languages.ObjCPP);
bool isCompileAsSwiftFile = subConfig.Languages.HasFlag(Languages.Swift);
bool isASMFileSection = subConfig.Languages.HasFlag(Languages.Asm);
bool isNASMFileSection = subConfig.Languages.HasFlag(Languages.Nasm);
bool isCompileAsCLRFile = subConfig.LanguageFeatures.HasFlag(LanguageFeatures.CLR);
bool isCompileAsNonCLRFile = subConfig.LanguageFeatures.HasFlag(LanguageFeatures.NonCLR);
bool isConsumeWinRTExtensions = subConfig.LanguageFeatures.HasFlag(LanguageFeatures.ConsumeWinRTExtensions) || (Options.GetObject<Options.Vc.Compiler.CompileAsWinRT>(conf) == Options.Vc.Compiler.CompileAsWinRT.Enable);
Expand Down Expand Up @@ -757,21 +759,47 @@ List<string> skipFiles
fastBuildUsingPlatformConfig = platformBff.CppConfigName(conf);
}

// TODOANT: Add nasm/masm change
if (isASMFileSection)
{
fastBuildUsingPlatformConfig += Template.ConfigurationFile.MasmConfigNameSuffix;
}
if (isNASMFileSection)
{
fastBuildUsingPlatformConfig += Template.ConfigurationFile.NasmConfigNameSuffix;
}

string fastBuildCompilerExtraOptions = Template.ConfigurationFile.CPPCompilerExtraOptions;
if (isASMFileSection)
{
fastBuildCompilerExtraOptions = Template.ConfigurationFile.MasmCompilerExtraOptions;
}
if (isNASMFileSection)
{
fastBuildCompilerExtraOptions = Template.ConfigurationFile.NasmCompilerExtraOptions;
}

string fastBuildCompilerExtraOptions = !isASMFileSection ? Template.ConfigurationFile.CPPCompilerExtraOptions : Template.ConfigurationFile.MasmCompilerExtraOptions;
string fastBuildCompilerOptionsDeoptimize = FileGeneratorUtilities.RemoveLineTag;
if (!isASMFileSection && conf.FastBuildDeoptimization != Project.Configuration.DeoptimizationWritableFiles.NoDeoptimization)
fastBuildCompilerOptionsDeoptimize = Template.ConfigurationFile.CPPCompilerOptionsDeoptimize;

string compilerOptions = !isASMFileSection ? Template.ConfigurationFile.CompilerOptionsCPP : Template.ConfigurationFile.CompilerOptionsMasm;
string compilerOptions = Template.ConfigurationFile.CompilerOptionsCPP;
if (isASMFileSection)
{
compilerOptions = Template.ConfigurationFile.CompilerOptionsMasm;
}
if (isNASMFileSection)
{
compilerOptions = Template.ConfigurationFile.CompilerOptionsNasm;
}
compilerOptions += Template.ConfigurationFile.CompilerOptionsCommon;

string compilerOptionsClang = Template.ConfigurationFile.CompilerOptionsClang +
Template.ConfigurationFile.CompilerOptionsCommon;
string compilerOptionsClang = Template.ConfigurationFile.CompilerOptionsClang;
if (isNASMFileSection)
{
compilerOptionsClang = Template.ConfigurationFile.CompilerOptionsNasm;
}
compilerOptionsClang += Template.ConfigurationFile.CompilerOptionsCommon;

string fastBuildDeoptimizationWritableFiles = null;
string fastBuildDeoptimizationWritableFilesWithToken = null;
Expand Down Expand Up @@ -1197,8 +1225,10 @@ List<string> skipFiles
{
if (isCompileAsSwiftFile)
applePlatformBff?.SetupSwiftOptions(bffGenerator);
else
else if (!isNASMFileSection)
clangPlatformBff?.SetupClangOptions(bffGenerator); // TODO: This checks twice if the platform supports Clang -- fix?
else
bffGenerator.Write(fastBuildCompilerExtraOptions);

if (conf.Platform.IsUsingClang())
{
Expand Down Expand Up @@ -1245,8 +1275,10 @@ List<string> skipFiles
{
if (isCompileAsSwiftFile)
applePlatformBff?.SetupSwiftOptions(bffGenerator);
else if (!isNASMFileSection)
clangPlatformBff?.SetupClangOptions(bffGenerator); // TODO: This checks twice if the platform supports Clang -- fix?
else
clangPlatformBff?.SetupClangOptions(bffGenerator); // TODO: This checks twice if the platform supports Clang -- fix?
bffGenerator.Write(fastBuildCompilerExtraOptions);

if (conf.Platform.IsUsingClang())
{
Expand Down Expand Up @@ -1343,16 +1375,17 @@ List<string> skipFiles
{
if (isCompileAsSwiftFile)
applePlatformBff?.SetupSwiftOptions(bffGenerator);
else
else if (!isNASMFileSection)
clangPlatformBff?.SetupClangOptions(bffGenerator); // TODO: This checks twice if the platform supports Clang -- fix?
else
bffGenerator.Write(fastBuildCompilerExtraOptions);;

if (conf.Platform.IsUsingClang())
{
if (isUsePrecomp)
bffGenerator.Write(Template.ConfigurationFile.PCHOptionsClang);

bffGenerator.Write(Template.ConfigurationFile.CompilerOptionsCommon);
bffGenerator.Write(Template.ConfigurationFile.CompilerOptionsClang);
bffGenerator.Write(compilerOptionsClang);
if (conf.FastBuildDeoptimization != Project.Configuration.DeoptimizationWritableFiles.NoDeoptimization)
{
if (isUsePrecomp)
Expand Down Expand Up @@ -1558,6 +1591,8 @@ private static void GenerateBffOptions(

var dependenciesInfo = FillLibrariesOptions(context);
dependenciesInfoPerConf.Add(context.Configuration, dependenciesInfo);

FillNasmOptions(context);
}

internal enum CompilerVersionForClangClType
Expand Down Expand Up @@ -1805,7 +1840,6 @@ private static void FillIncludeDirectoriesOptions(BffGenerationContext context)
// Fill Assembly include dirs
var assemblyDirs = new List<string>();
assemblyDirs.AddRange(assemblyIncludePaths.Select(p => CmdLineConvertIncludePathsFunc(context, context.EnvironmentVariableResolver, p, defaultCmdLineIncludePrefix)));

if (assemblyDirs.Any())
context.CommandLineOptions["AdditionalAssemblyIncludeDirectories"] = string.Join($"'{Environment.NewLine} + ' ", assemblyDirs);

Expand Down Expand Up @@ -1930,6 +1964,54 @@ private static DependenciesInfo FillLibrariesOptions(BffGenerationContext contex
return dependenciesInfo;
}

private static void FillNasmOptions(BffGenerationContext context)
{
// Compiler path for nasm
context.CommandLineOptions["PathExe"] = context.Project.NasmExePath;

// Pre included files for NASM syntax
var preIncludedFiles = new List<string>();
preIncludedFiles.AddRange(context.Project.NasmPreIncludedFiles.Select(p => "-P\"" + p + "\""));
string preIncludedFilesJoined = string.Join(' ', preIncludedFiles);
context.CommandLineOptions["PreIncludedFiles"] = preIncludedFilesJoined;

// Fill Assembly include dirs in nasm syntax
var nasmAssemblyDirs = new List<string>();
var platformVcxproj = PlatformRegistry.Query<IPlatformVcxproj>(context.Configuration.Platform);
var assemblyIncludePaths = new OrderableStrings(platformVcxproj.GetAssemblyIncludePaths(context));
nasmAssemblyDirs.AddRange(assemblyIncludePaths.Select(p => CmdLineConvertIncludePathsFunc(context, context.EnvironmentVariableResolver, p, "-I ")));

if (nasmAssemblyDirs.Any())
{
context.CommandLineOptions["AdditionalAssemblyNasmIncludeDirectories"] = string.Join($"'{Environment.NewLine} + ' ", nasmAssemblyDirs);
}
else
{
context.CommandLineOptions["AdditionalAssemblyNasmIncludeDirectories"] = FileGeneratorUtilities.RemoveLineTag;
}

// Defines in NASM syntax
var defines = new Strings();
defines.AddRange(context.Options.ExplicitDefines);
defines.AddRange(context.Configuration.Defines);

if (defines.Count > 0)
{
var fastBuildNasmDefines = new List<string>();

foreach (string define in defines.SortedValues)
{
if (!string.IsNullOrWhiteSpace(define))
fastBuildNasmDefines.Add(string.Format(@"{0}{1}{2}{1}", "-D", Util.DoubleQuotes, define.Replace(Util.DoubleQuotes, Util.EscapedDoubleQuotes)));
}
context.CommandLineOptions["NasmPreprocessorDefinitions"] = string.Join($"'{Environment.NewLine} + ' ", fastBuildNasmDefines);
}
else
{
context.CommandLineOptions["NasmPreprocessorDefinitions"] = FileGeneratorUtilities.RemoveLineTag;
}
}

private static void SelectAdditionalLibraryDirectoriesOption(BffGenerationContext context)
{
// TODO: really not ideal, refactor and move the properties we need from it someplace else
Expand Down Expand Up @@ -2324,9 +2406,11 @@ out List<Vcxproj.ProjectFile> filesInNonDefaultSections
conf.ResolvedSourceFilesWithCompileAsWinRTOption.Contains(file.FileName)) &&
!(conf.ExcludeWinRTExtensions.Contains(file.FileName) ||
conf.ResolvedSourceFilesWithExcludeAsWinRTOption.Contains(file.FileName));
// TODOANT: Also trigger on .nasm files
bool isASMFile = string.Compare(file.FileExtension, ".asm", StringComparison.OrdinalIgnoreCase) == 0;
bool isSwiftFile = string.Compare(file.FileExtension, ".swift", StringComparison.OrdinalIgnoreCase) == 0 &&
(PlatformRegistry.Query<IApplePlatformBff>(conf.Platform)?.IsSwiftSupported() ?? false);
bool isNASMFile = string.Compare(file.FileExtension, ".nasm", StringComparison.OrdinalIgnoreCase) == 0;

Options.Vc.Compiler.Exceptions exceptionSetting = conf.GetExceptionSettingForFile(file.FileName);

Expand Down Expand Up @@ -2354,6 +2438,8 @@ out List<Vcxproj.ProjectFile> filesInNonDefaultSections
languageKind |= Languages.Swift;
if (isASMFile)
languageKind |= Languages.Asm;
if (isNASMFile)
languageKind |= Languages.Nasm;

LanguageFeatures languageFeatures = LanguageFeatures.None;
if (isCompileAsCLRFile)
Expand Down
9 changes: 9 additions & 0 deletions Sharpmake.Generators/FastBuild/MasterBff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@ private static void WriteMasterCompilerSection(FileGenerator masterBffGenerator,
using (masterBffGenerator.Declare("fastBuildResourceCompilerName", compConf.ResourceCompiler != FileGeneratorUtilities.RemoveLineTag ? "RC" + compilerConfiguration.Key : FileGeneratorUtilities.RemoveLineTag))
using (masterBffGenerator.Declare("fastBuildMasmCompiler", compConf.Masm))
using (masterBffGenerator.Declare("fastBuildMasmCompilerName", "ML" + compilerConfiguration.Key))

// TODOANT make sure we have nasm compiler found and used.
using (masterBffGenerator.Declare("fastBuildNasmCompiler", compConf.Nasm))
using (masterBffGenerator.Declare("fastBuildNasmCompilerName", "Nasm" + compilerConfiguration.Key))

using (masterBffGenerator.Declare("fastBuildCompilerName", compConf.Compiler != FileGeneratorUtilities.RemoveLineTag ? compConf.Compiler : compiler.Key))
using (masterBffGenerator.Declare("fastBuildLibrarian", compConf.Librarian))
using (masterBffGenerator.Declare("fastBuildLinker", compConf.Linker))
Expand All @@ -760,6 +765,10 @@ private static void WriteMasterCompilerSection(FileGenerator masterBffGenerator,
if (!string.IsNullOrEmpty(compConf.Masm))
masterBffGenerator.Write(Bff.Template.ConfigurationFile.MasmCompilerSettings);

// TODOANT
if (!string.IsNullOrEmpty(compConf.Nasm))
masterBffGenerator.Write(Bff.Template.ConfigurationFile.NasmCompilerSettings);

masterBffGenerator.Write(Bff.Template.ConfigurationFile.CompilerConfiguration);
}
}
Expand Down
1 change: 1 addition & 0 deletions Sharpmake.Generators/VisualStudio/IPlatformVcxproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public interface IPlatformVcxproj
void GenerateProjectCompileVcxproj(IVcxprojGenerationContext context, IFileGenerator generator);
void GenerateProjectLinkVcxproj(IVcxprojGenerationContext context, IFileGenerator generator);
void GenerateProjectMasmVcxproj(IVcxprojGenerationContext context, IFileGenerator generator);
void GenerateProjectNasmVcxproj(IVcxprojGenerationContext context, IFileGenerator generator);
void GenerateUserConfigurationFile(Project.Configuration conf, IFileGenerator generator); // Should take IVcxprojGenerationContext but this is called by BaseUserFile which should not know that interface.
void GenerateRunFromPcDeployment(IVcxprojGenerationContext context, IFileGenerator generator);

Expand Down
Loading

0 comments on commit 52078f8

Please sign in to comment.