Skip to content

Commit 7962a0d

Browse files
committed
feat: building with LUA_COMPAT_* definition as Unix, and exporting it to the pkg-config file.
1 parent 9d6c90c commit 7962a0d

File tree

6 files changed

+209
-18
lines changed

6 files changed

+209
-18
lines changed

.github/workflows/lfs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ jobs:
101101
$console = Join-Path "${{ env.CONSOLE_DIR }}" -ChildPath "LuaInstaller.Console.exe";
102102
103103
$commands = "/?", "list-win-sdk", "list-vs-${{ inputs.arch }}", "list-lua";
104-
$color = (0x1b -as [char]) + "[35m";
104+
$color = (0x1b -as [char]) + "[36m";
105105
106106
foreach ($c in $commands) {
107107
Write-Host;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.IO;
2+
3+
namespace LuaInstaller.Core
4+
{
5+
public abstract class AbstractLuaCompatibility
6+
{
7+
protected internal readonly string _luaCompat;
8+
9+
protected AbstractLuaCompatibility(string luaCompat)
10+
{
11+
_luaCompat = luaCompat;
12+
}
13+
14+
public bool HasCompatibility
15+
{
16+
get
17+
{
18+
return _luaCompat != null;
19+
}
20+
}
21+
22+
public bool TryAddDefine(ICompiler compiler)
23+
{
24+
bool res;
25+
if (res = (compiler != null && HasCompatibility))
26+
{
27+
compiler.AddDefine(_luaCompat);
28+
}
29+
30+
return res;
31+
}
32+
}
33+
}

LuaInstaller.Core/InstallationManager.cs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45

56
namespace LuaInstaller.Core
67
{
@@ -36,6 +37,50 @@ public InstallationManager(ICompiler compiler, ILinker linker)
3637
_objExtension = _compiler.DefaultObjectExtension;
3738
}
3839

40+
private AbstractLuaCompatibility GetLuaCompatibility(string srcDir)
41+
{
42+
bool result = false;
43+
string luaCompat = null;
44+
45+
string srcMakefile = Path.Combine(srcDir, "Makefile");
46+
47+
if (File.Exists(srcMakefile))
48+
{
49+
using (FileStream fileStream = File.OpenRead(srcMakefile))
50+
using (StreamReader Makefile = new StreamReader(fileStream))
51+
{
52+
string line = null;
53+
Match match = null;
54+
Regex regex = new Regex(@"\-D(LUA_COMPAT_[a-zA-Z0-9_]+)\b");
55+
56+
while (!(result || Makefile.EndOfStream))
57+
{
58+
line = Makefile.ReadLine();
59+
60+
match = regex.Match(line);
61+
62+
if (match.Success)
63+
{
64+
luaCompat = match.Groups[1].Value;
65+
66+
result = true;
67+
}
68+
}
69+
}
70+
}
71+
72+
AbstractLuaCompatibility compatibility;
73+
if (result)
74+
{
75+
compatibility = new LuaCompatibility(luaCompat);
76+
}
77+
else
78+
{
79+
compatibility = new LuaNoCompatibility();
80+
}
81+
return compatibility;
82+
}
83+
3984
private void LinkDll(string srcDir, string outputFile, VisualStudio vs, WindowsSdk winsdk)
4085
{
4186
try
@@ -72,14 +117,16 @@ private void LinkDll(string srcDir, string outputFile, VisualStudio vs, WindowsS
72117
_linker.Reset();
73118
}
74119
}
75-
private void BuildDll(string srcDir, string outputFile, VisualStudio vs, WindowsSdk winsdk)
120+
private void BuildDll(string srcDir, string outputFile, VisualStudio vs, WindowsSdk winsdk, AbstractLuaCompatibility luaCompat)
76121
{
77122
string buildDir = Path.Combine(
78123
Path.GetTempPath(), "li-build-dll-" + Guid.NewGuid().ToString("N")
79124
);
80125

81126
try
82127
{
128+
luaCompat.TryAddDefine(_compiler);
129+
83130
_compiler.AddDefine("LUA_BUILD_AS_DLL");
84131

85132
foreach (string inc in vs.IncludeDirectories)
@@ -167,14 +214,16 @@ private void LinkInterpreter(string srcDir, string luaLibPath, string outputFile
167214
_linker.Reset();
168215
}
169216
}
170-
private void BuildInterpreter(string srcDir, string luaLibPath, string outputFile, VisualStudio vs, WindowsSdk winsdk)
217+
private void BuildInterpreter(string srcDir, string luaLibPath, string outputFile, VisualStudio vs, WindowsSdk winsdk, AbstractLuaCompatibility luaCompat)
171218
{
172219
string buildDir = Path.Combine(
173220
Path.GetTempPath(), "li-interpreter-" + Guid.NewGuid().ToString("N")
174221
);
175222

176223
try
177224
{
225+
luaCompat.TryAddDefine(_compiler);
226+
178227
foreach (string inc in vs.IncludeDirectories)
179228
{
180229
_compiler.AddIncludeDirectory(inc);
@@ -258,14 +307,16 @@ private void LinkCompiler(string srcDir, string outputFile, VisualStudio vs, Win
258307
_linker.Reset();
259308
}
260309
}
261-
private void BuildCompiler(string srcDir, string outputFile, VisualStudio vs, WindowsSdk winsdk)
310+
private void BuildCompiler(string srcDir, string outputFile, VisualStudio vs, WindowsSdk winsdk, AbstractLuaCompatibility luaCompat)
262311
{
263312
string buildDir = Path.Combine(
264313
Path.GetTempPath(), "li-build-compiler-" + Guid.NewGuid().ToString("N")
265314
);
266315

267316
try
268317
{
318+
luaCompat.TryAddDefine(_compiler);
319+
269320
foreach (string inc in vs.IncludeDirectories)
270321
{
271322
_compiler.AddIncludeDirectory(inc);
@@ -392,11 +443,11 @@ private void InstallOnDestDir(LuaDestinationDirectory workDir, LuaDestinationDir
392443
}
393444
}
394445

395-
private void CreatePkgConfigFile(LuaDestinationDirectory destDir, LuaVersion version, LuaGeneratedBinaries generatedBinaries)
446+
private void CreatePkgConfigFile(LuaDestinationDirectory destDir, LuaVersion version, LuaGeneratedBinaries generatedBinaries, AbstractLuaCompatibility luaCompat)
396447
{
397448
try
398449
{
399-
PkgConfigOutputInfo pkgConfigOutput = new PkgConfigOutputInfo(version, generatedBinaries, destDir);
450+
PkgConfigOutputInfo pkgConfigOutput = new PkgConfigOutputInfo(version, generatedBinaries, destDir, luaCompat);
400451

401452
if (pkgConfigOutput.WritePkgConfigFile())
402453
{
@@ -494,21 +545,25 @@ public void Build(LuaVersion version, string luaDestDir, VisualStudio vs, Window
494545
LuaSourcesDirectory sourcesDir = new LuaSourcesDirectory(luaSourcesDir);
495546
LuaDestinationDirectory workDir = new LuaDestinationDirectory(luaWorkDir);
496547
LuaGeneratedBinaries generatedBinaries = new LuaGeneratedBinaries(version);
548+
549+
AbstractLuaCompatibility luaCompat = GetLuaCompatibility(sourcesDir.Src);
497550

498551
workDir.CreateFrom(sourcesDir);
499552

500553
BuildDll(
501554
sourcesDir.Src,
502555
Path.Combine(workDir.Lib, generatedBinaries.DllName),
503556
vs,
504-
winsdk
557+
winsdk,
558+
luaCompat
505559
);
506560
BuildInterpreter(
507561
sourcesDir.Src,
508562
Path.Combine(workDir.Lib, generatedBinaries.ImportLibName),
509563
Path.Combine(workDir.Bin, generatedBinaries.InterpreterName),
510564
vs,
511-
winsdk
565+
winsdk,
566+
luaCompat
512567
);
513568
File.Copy(
514569
Path.Combine(workDir.Lib, generatedBinaries.DllName),
@@ -519,7 +574,8 @@ public void Build(LuaVersion version, string luaDestDir, VisualStudio vs, Window
519574
sourcesDir.Src,
520575
Path.Combine(workDir.Bin, generatedBinaries.CompilerName),
521576
vs,
522-
winsdk
577+
winsdk,
578+
luaCompat
523579
);
524580

525581
if (File.Exists(Path.Combine(workDir.Lib, generatedBinaries.DllName)))
@@ -531,7 +587,7 @@ public void Build(LuaVersion version, string luaDestDir, VisualStudio vs, Window
531587

532588
LuaDestinationDirectory destDir = new LuaDestinationDirectory(luaDestDir);
533589
InstallOnDestDir(workDir, destDir);
534-
CreatePkgConfigFile(destDir, version, generatedBinaries);
590+
CreatePkgConfigFile(destDir, version, generatedBinaries, luaCompat);
535591
SetEnvironmentVariables(destDir, variableTarget);
536592
}
537593
finally
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace LuaInstaller.Core
4+
{
5+
public sealed class LuaCompatibility : AbstractLuaCompatibility
6+
{
7+
internal LuaCompatibility(string luaCompat) : base(luaCompat)
8+
{
9+
}
10+
11+
public string Value
12+
{
13+
get
14+
{
15+
return _luaCompat;
16+
}
17+
}
18+
19+
public void AddDefine(ICompiler compiler)
20+
{
21+
if (compiler == null)
22+
{
23+
throw new ArgumentNullException();
24+
}
25+
26+
compiler.AddDefine(Value);
27+
}
28+
}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace LuaInstaller.Core
2+
{
3+
public sealed class LuaNoCompatibility : AbstractLuaCompatibility
4+
{
5+
public LuaNoCompatibility() : base(null)
6+
{
7+
}
8+
}
9+
}

LuaInstaller.Core/PkgConfigOutputInfo.cs

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ static PkgConfigOutputInfo()
1515
private readonly LuaVersion _version;
1616
private readonly LuaGeneratedBinaries _generatedBinaries;
1717
private readonly LuaDestinationDirectory _destinationDir;
18+
private readonly AbstractLuaCompatibility _luaCompat;
1819

19-
20-
public PkgConfigOutputInfo(LuaVersion version, LuaGeneratedBinaries generatedBinaries, LuaDestinationDirectory luaDestinationDirectory)
20+
public PkgConfigOutputInfo(
21+
LuaVersion version,
22+
LuaGeneratedBinaries generatedBinaries,
23+
LuaDestinationDirectory luaDestinationDirectory,
24+
AbstractLuaCompatibility luaCompat
25+
)
2126
{
2227
if (version == null)
2328
{
@@ -34,9 +39,15 @@ public PkgConfigOutputInfo(LuaVersion version, LuaGeneratedBinaries generatedBin
3439
throw new ArgumentNullException("luaDestinationDirectory");
3540
}
3641

42+
if (luaCompat == null)
43+
{
44+
throw new ArgumentNullException("luaCompat");
45+
}
46+
3747
_version = version;
3848
_generatedBinaries = generatedBinaries;
3949
_destinationDir = luaDestinationDirectory;
50+
_luaCompat = luaCompat;
4051
}
4152

4253
public string Prefix
@@ -103,6 +114,58 @@ public string VersionShort
103114
}
104115
}
105116

117+
public string Name
118+
{
119+
get
120+
{
121+
return "Lua";
122+
}
123+
}
124+
125+
public string Description
126+
{
127+
get
128+
{
129+
return "Lua language engine";
130+
}
131+
}
132+
133+
public string Libs
134+
{
135+
get
136+
{
137+
return "-L${libdir} -l${lib_name}";
138+
}
139+
}
140+
141+
public string Requires
142+
{
143+
get
144+
{
145+
return string.Empty;
146+
}
147+
}
148+
149+
public string Cflags
150+
{
151+
get
152+
{
153+
string compatibilitySwitch = _luaCompat.HasCompatibility ?
154+
string.Format(" -D{0}", ((LuaCompatibility)_luaCompat).Value) :
155+
string.Empty;
156+
157+
return "-I${includedir}" + compatibilitySwitch;
158+
}
159+
}
160+
161+
public AbstractLuaCompatibility LuaCompat
162+
{
163+
get
164+
{
165+
return _luaCompat;
166+
}
167+
}
168+
106169
public string InstallBin
107170
{
108171
get
@@ -173,12 +236,13 @@ public void Write(Stream stream)
173236
writer.WriteLine(string.Format("INSTALL_LMOD={0}", InstallLuaModules.Replace('\\', '/')));
174237
writer.WriteLine(string.Format("INSTALL_CMOD={0}", InstallCModules.Replace('\\', '/')));
175238
writer.WriteLine();
176-
writer.WriteLine("Name: Lua");
177-
writer.WriteLine("Description: Lua language engine");
178-
writer.WriteLine("Version: ${R}");
179-
writer.WriteLine("Requires:");
180-
writer.WriteLine("Libs: -L${libdir} -l${lib_name}");
181-
writer.WriteLine("Cflags: -I${includedir}");
239+
writer.WriteLine(string.Format("Name: {0}", Name));
240+
writer.WriteLine(string.Format("Description: {0}", Description));
241+
writer.WriteLine(string.Format("Version: {0}", Version));
242+
writer.WriteLine(string.Format("Requires: {0}", Requires));
243+
writer.WriteLine(string.Format("Libs: {0}", Libs));
244+
writer.WriteLine(string.Format("Cflags: {0}", Cflags));
245+
182246
writer.WriteLine();
183247
}
184248
}

0 commit comments

Comments
 (0)