@@ -103,6 +103,72 @@ public func describe(prefix: String, _ conf: Configuration, _ modules: [Module],
103
103
}
104
104
}
105
105
106
+ //For C language Modules
107
+ //FIXME: Probably needs more compiler options for debug and release modes
108
+ //FIXME: Incremental builds
109
+ //FIXME: Add support for executables
110
+ for case let module as ClangModule in modules {
111
+
112
+ //FIXME: Generate modulemaps if possible
113
+ //Since we're not generating modulemaps currently we'll just emit empty module map file
114
+ //if it not present
115
+ if !module. moduleMapPath. isFile {
116
+ try mkdir ( module. moduleMapPath. parentDirectory)
117
+ try fopen ( module. moduleMapPath, mode: . Write) { fp in
118
+ try fputs ( " \n " , fp)
119
+ }
120
+ }
121
+
122
+ let inputs = module. dependencies. map { $0. targetName } + module. sources. paths
123
+ let productPath = Path . join ( prefix, " lib \( module. c99name) .so " )
124
+ let wd = Path . join ( prefix, " \( module. c99name) .build " )
125
+ mkdirs. insert ( wd)
126
+
127
+ var args : [ String ] = [ ]
128
+ #if os(Linux)
129
+ args += [ " -fPIC " ]
130
+ #endif
131
+ args += [ " -fmodules " , " -fmodule-name= \( module. name) " ]
132
+ args += [ " -L \( prefix) " ]
133
+
134
+ for case let dep as ClangModule in module. dependencies {
135
+ let includeFlag : String
136
+ //add `-iquote` argument to the include directory of every target in the package in the
137
+ //transitive closure of the target being built allowing the use of `#include "..."`
138
+ //add `-I` argument to the include directory of every target outside the package in the
139
+ //transitive closure of the target being built allowing the use of `#include <...>`
140
+ //FIXME: To detect external deps we're checking if their path's parent.parent directory
141
+ //is `Packages` as external deps will get copied to `Packages` dir. There should be a
142
+ //better way to do this.
143
+ if dep. path. parentDirectory. parentDirectory. basename == " Packages " {
144
+ includeFlag = " -I "
145
+ } else {
146
+ includeFlag = " -iquote "
147
+ }
148
+ args += [ includeFlag, dep. path]
149
+ args += [ " -l \( dep. c99name) " ] //FIXME: giving path to other module's -fmodule-map-file is not linking that module
150
+ }
151
+
152
+ switch conf {
153
+ case . Debug:
154
+ args += [ " -g " , " -O0 " ]
155
+ case . Release:
156
+ args += [ " -O2 " ]
157
+ }
158
+
159
+ args += module. sources. paths
160
+ args += [ " -shared " , " -o " , productPath]
161
+
162
+ let clang = ShellTool (
163
+ description: " Compiling \( module. name) " ,
164
+ inputs: inputs,
165
+ outputs: [ productPath, module. targetName] ,
166
+ args: [ Toolchain . clang] + args)
167
+
168
+ let command = Command ( name: module. targetName, tool: clang)
169
+ append ( command, buildable: module)
170
+ }
171
+
106
172
// make eg .build/debug/foo.build/subdir for eg. Sources/foo/subdir/bar.swift
107
173
// TODO swift-build-tool should do this
108
174
for dir in mkdirs {
@@ -171,6 +237,7 @@ public func describe(prefix: String, _ conf: Configuration, _ modules: [Module],
171
237
}
172
238
args += platformArgs ( ) //TODO don't need all these here or above: split outname
173
239
args += Xld
240
+ args += [ " -L \( prefix) " ]
174
241
args += [ " -o " , outpath]
175
242
args += objects
176
243
0 commit comments