@@ -25,14 +25,15 @@ class RunCBuilder {
2525 final Uri outDir;
2626 final Target target;
2727
28- /// The install of the [dynamicLibrary] .
28+ /// The install name of the [dynamicLibrary] .
2929 ///
3030 /// Can be inspected with `otool -D <path-to-dylib>` .
3131 ///
3232 /// Can be modified with `install_name_tool` .
3333 final Uri ? installName;
3434
3535 final Map <String , String ?> defines;
36+ final bool ? pic;
3637
3738 RunCBuilder ({
3839 required this .buildConfig,
@@ -43,6 +44,7 @@ class RunCBuilder {
4344 this .staticLibrary,
4445 this .installName,
4546 this .defines = const {},
47+ this .pic,
4648 }) : outDir = buildConfig.outDir,
4749 target = buildConfig.target,
4850 assert ([executable, dynamicLibrary, staticLibrary]
@@ -145,6 +147,20 @@ class RunCBuilder {
145147 '-o' ,
146148 outDir.resolve ('out.o' ).toFilePath (),
147149 ],
150+ if (pic != null )
151+ if (pic! ) ...[
152+ if (dynamicLibrary != null ) '-fPIC' ,
153+ // Using PIC for static libraries allows them to be linked into
154+ // any executable, but it is not necessarily the best option in
155+ // terms of overhead. We would have to know wether the target into
156+ // which the static library is linked is PIC, PIE or neither. Then
157+ // we could use the same option for the static library.
158+ if (staticLibrary != null ) '-fPIC' ,
159+ if (executable != null ) '-fPIE' ,
160+ ] else ...[
161+ '-fno-PIC' ,
162+ '-fno-PIE' ,
163+ ],
148164 for (final MapEntry (key: name, : value) in defines.entries)
149165 if (value == null ) '-D$name ' else '-D$name =$value ' ,
150166 ],
0 commit comments