-
Notifications
You must be signed in to change notification settings - Fork 5
/
Shake_Methcla.hs
538 lines (473 loc) · 21.8 KB
/
Shake_Methcla.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
-- Copyright 2012-2014 Samplecount S.L.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE TypeOperators #-}
module Shake_Methcla (
buildSystemVersion
, Variant(..)
, Options
, buildConfig
, defaultOptions
, optionDescrs
, Config(..)
, mkRules
, LibraryTarget(..)
, libmethcla
) where
import Control.Arrow
import Control.Monad
import Data.Char (toLower)
import Development.Shake as Shake
import Development.Shake.FilePath
import Development.Shake.Language.C
import qualified Development.Shake.Language.C.BuildFlags as BuildFlags
import qualified Development.Shake.Language.C.Host as Host
import Development.Shake.Language.C.Label
import qualified Development.Shake.Language.C.PkgConfig as PkgConfig
import qualified Development.Shake.Language.C.Target.Android as Android
import qualified Development.Shake.Language.C.Target.NaCl as NaCl
import qualified Development.Shake.Language.C.Target.OSX as OSX
import qualified Development.Shake.Language.C.ToolChain as ToolChain
import qualified Development.Shake.Language.C.Config as Config
import System.Console.GetOpt
import Text.Read (readMaybe)
{-import Debug.Trace-}
-- Bump this when build system changes require a complete rebuild.
buildSystemVersion :: String
buildSystemVersion = "1"
-- ====================================================================
-- Utilities
-- lookupEnv :: String -> IO (Maybe String)
-- lookupEnv name = do
-- e <- E.try $ Env.getEnv name :: IO (Either E.SomeException String)
-- case e of
-- Left _ -> return Nothing
-- Right value -> return $ Just value
--
-- getEnv_ :: String -> IO String
-- getEnv_ = fmap (maybe "" id) . lookupEnv
getEnv' :: String -> Action String
getEnv' name =
getEnvWithDefault
(error $ "Environment variable " ++ name ++ " is undefined")
name
readConfigVar :: (Read a) => (String -> Action (Maybe String)) -> String -> Action a
readConfigVar config key =
maybe (error $ key ++ " undefined")
(maybe (error $ "Couldn't parse " ++ key) id . readMaybe)
<$> config key
-- ====================================================================
-- Library variants
data Variant = Default | Pro deriving (Eq, Show)
isPro :: Variant -> Bool
isPro Pro = True
isPro _ = False
newtype VersionHeader = VersionHeader { versionHeaderPath :: FilePath }
mkVersionHeader :: FilePath -> VersionHeader
mkVersionHeader buildDir = VersionHeader $ buildDir </> "include/Methcla/Version.h"
versionHeaderRule :: Variant -> FilePath -> FilePath -> Rules VersionHeader
versionHeaderRule variant sourceDir buildDir = do
let output = mkVersionHeader buildDir
versionHeaderPath output %> \path -> do
version <- head <$> readFileLines (sourceDir </> "VERSION")
let tag = if isPro variant then "-pro" else ""
writeFileChanged path $ unlines [
"#ifndef METHCLA_VERSION_H_INCLUDED"
, "#define METHCLA_VERSION_H_INCLUDED"
, ""
, "static const char* kMethclaVersion = \"" ++ version ++ tag ++ "\";"
, ""
, "#endif /* METHCLA_VERSION_H_INCLUDED */"
]
return output
-- ====================================================================
-- Configurations
data Config = Debug | Release deriving (Eq, Show)
instance ToBuildPrefix Config where
toBuildPrefix = map toLower . show
parseConfig :: String -> Either String Config
parseConfig x =
case map toLower x of
"debug" -> Right Debug
"release" -> Right Release
_ -> Left $ "Invalid configuration `" ++ x ++ "'"
-- ====================================================================
-- Commandline targets
data Options = Options {
_buildConfig :: Config
, _toolChainVariant :: ToolChain.ToolChainVariant
} deriving (Show)
buildConfig :: Options :-> Config
buildConfig = lens _buildConfig
(\g f -> f { _buildConfig = g (_buildConfig f) })
toolChainVariant :: Options :-> ToolChain.ToolChainVariant
toolChainVariant = lens _toolChainVariant
(\g f -> f { _toolChainVariant = g (_toolChainVariant f) })
defaultOptions :: Options
defaultOptions = Options {
_buildConfig = Debug
, _toolChainVariant = ToolChain.Generic
}
optionDescrs :: [OptDescr (Either String (Options -> Options))]
optionDescrs = [ Option "c" ["config"]
(ReqArg (fmap (set buildConfig) . parseConfig) "CONFIG")
"Build configuration (debug, release)."
, Option "" ["toolchain"]
(ReqArg (fmap (set toolChainVariant) . parseToolChainVariant) "generic|gcc|llvm")
"Toolchain variant (generic, gcc, llvm)." ]
where
parseToolChainVariant s =
case s of
"generic" -> Right ToolChain.Generic
"gcc" -> Right ToolChain.GCC
"llvm" -> Right ToolChain.LLVM
_ -> Left $ "Invalid toolchain variant " ++ show s
mapTarget :: Monad m => (Arch -> Target) -> [Arch] -> (Target -> m a) -> m [a]
mapTarget mkTarget archs f = mapM (f . mkTarget) archs
mkBuildPrefix :: FilePath -> Config -> String -> FilePath
mkBuildPrefix buildDir config platform =
buildDir
</> toBuildPrefix config
</> platform
platformBuildPrefix :: FilePath -> Config -> Platform -> FilePath
platformBuildPrefix buildDir config platform =
mkBuildPrefix buildDir config (toBuildPrefix platform)
targetBuildPrefix :: FilePath -> Config -> Target -> FilePath
targetBuildPrefix buildDir config target =
mkBuildPrefix buildDir config (toBuildPrefix target)
data LibraryTarget =
Lib_Pepper
| Lib_Android Arch
| Lib_iOS
| Lib_Desktop
deriving (Eq, Show)
-- selectLibraryFlags :: (BuildFlags -> BuildFlags) -> (BuildFlags -> BuildFlags)
-- selectLibraryFlags f =
-- let b = f mempty
-- in set libraries (get libraries b)
-- . set linkerFlags (get linkerFlags b)
-- $ mempty
libmethcla :: LibraryTarget -> Variant -> Config -> FilePath -> FilePath -> Maybe PkgConfig.Options -> Rules (FilePath, Action (BuildFlags -> BuildFlags))
libmethcla libTarget variant config sourceDir buildDir pkgConfigOptions = do
let (result, exportFlags) =
case libTarget of
Lib_Pepper ->
( targetBuildPrefix buildDir config NaCl.target </> "libmethcla.a"
, -- FIXME: Read this from config file
PkgConfig.pkgConfig (maybe PkgConfig.defaultOptions id pkgConfigOptions) "sndfile" )
Lib_iOS ->
( mkBuildPrefix buildDir config "iphone-universal" </> "libmethcla.a"
, return id )
Lib_Android arch ->
( mkBuildPrefix buildDir config "android"
</> Android.abiString arch
</> "libmethcla.a"
, -- FIXME: Read this from config file
PkgConfig.pkgConfig (maybe PkgConfig.defaultOptions id pkgConfigOptions) "sndfile" )
Lib_Desktop ->
( targetBuildPrefix buildDir config (fst Host.defaultToolChain) </> "libmethcla.a"
, return id )
result %> \_ -> do
alwaysRerun
currentShakeOptions <- getShakeOptions
liftIO $ do
let options = currentShakeOptions {
shakeFiles = addTrailingPathSeparator buildDir
, shakeVersion = buildSystemVersion
, shakeReport = map (combine buildDir . takeFileName)
(shakeReport currentShakeOptions)
}
rules = mkRules variant
sourceDir
buildDir
(set buildConfig config defaultOptions)
pkgConfigOptions
shake options $ rules >> want [result]
return (result, pure ( append systemIncludes [sourceDir </> "include"]
. append localLibraries [result])
>>>= exportFlags)
mkRules :: Variant -> FilePath -> FilePath -> Options -> Maybe PkgConfig.Options -> Rules ()
mkRules variant sourceDir buildDir options pkgConfigOptions = do
let config = get buildConfig options
targetBuildPrefix' target = targetBuildPrefix buildDir config target
-- Common rules
phony "clean" $ removeFilesAfter buildDir ["//*"]
-- Update includes (because we can't use symlinks with git on Windows)
phony "update-includes" $
cmd "rsync -a external_libraries/oscpp/include/oscpp/ include/oscpp/"
want ["update-includes"]
_ <- versionHeaderRule variant sourceDir buildDir
let configEnv = [
("la.methc.sourceDir", sourceDir)
, ("la.methc.buildDir", buildDir)
, ("la.methc.buildConfig", map toLower (show config))
, ("la.methc.variantDir", if isPro variant
then sourceDir </> "pro/config"
else sourceDir </> "config/default") ]
getConfigFromWithEnv <- do
f <- Config.mkConfig
return $ \deps file env key -> f deps (sourceDir </> file) (configEnv ++ env) key
let getBuildFlags cfg =
BuildFlags.fromConfig cfg
>>>= PkgConfig.fromConfig
(maybe PkgConfig.defaultOptions id pkgConfigOptions) cfg
getSources cfg = do
need =<< Config.getPaths cfg [ "Sources.deps" ]
Config.getPaths cfg [ "Sources" ]
-- iOS
do
let sdkVersion platform = maximum <$> OSX.getPlatformVersions platform
getConfig = getConfigFromWithEnv [] "config/ios.cfg" []
iphoneosLibs <- mapTarget (OSX.target OSX.iPhoneOS) [Arm Armv7, Arm Arm64] $ \target -> do
staticLibrary
(OSX.toolChain
<$> OSX.getSDKRoot
<*> sdkVersion OSX.iPhoneOS
<*> pure target
>>= applyEnv)
(targetBuildPrefix' target </> "libmethcla.a")
(getBuildFlags getConfig)
(getSources getConfig)
let iphoneosLib = platformBuildPrefix buildDir config OSX.iPhoneOS </> "libmethcla.a"
iphoneosLib %> OSX.universalBinary iphoneosLibs
phony "iphoneos" (need [iphoneosLib])
iphonesimulatorLibs <- mapTarget (OSX.target OSX.iPhoneSimulator) [X86 I386, X86 X86_64] $ \target -> do
staticLibrary
(OSX.toolChain
<$> OSX.getSDKRoot
<*> sdkVersion OSX.iPhoneSimulator
<*> pure target
>>= applyEnv)
(targetBuildPrefix' target </> "libmethcla.a")
(getBuildFlags getConfig)
(getSources getConfig)
let iphonesimulatorLib = platformBuildPrefix buildDir config OSX.iPhoneSimulator </> "libmethcla.a"
iphonesimulatorLib %> OSX.universalBinary iphonesimulatorLibs
phony "iphonesimulator" (need [iphonesimulatorLib])
let universalTarget = "iphone-universal"
universalLib = mkBuildPrefix buildDir config universalTarget </> "libmethcla.a"
universalLib %> OSX.universalBinary (iphoneosLibs ++ [iphonesimulatorLib])
phony universalTarget (need [universalLib])
-- Android
do
let ndk = getEnv' "ANDROID_NDK"
let mkAndroidConfig file key = do
ndkPath <- ndk
getConfigFromWithEnv [] file [("ANDROID_NDK", ndkPath)] key
libs <- mapTarget Android.target [Arm Armv5, Arm Armv7, Arm Arm64, X86 I386] $ \target -> do
let getConfig = mkAndroidConfig "config/android.cfg"
abi = Android.abiString (targetArch target)
toolChain = Android.toolChain
<$> ndk
<*> (Android.sdkVersion <$> readConfigVar getConfig "Android.sdkVersion")
<*> pure LLVM
<*> pure target
libmethcla <- staticLibrary toolChain
(targetBuildPrefix' target </> "libmethcla.a")
( (Android.libcxx Static <$> ndk <*> pure target)
>>>= (getBuildFlags getConfig))
(getSources getConfig)
let getConfig = mkAndroidConfig "config/android_tests.cfg"
libmethcla_tests <- sharedLibrary toolChain
(targetBuildPrefix' target </> "libmethcla-tests.so")
( (Android.libcxx Static <$> ndk <*> pure target)
>>>= (getBuildFlags getConfig)
>>>= pure (append localLibraries [libmethcla]))
(getSources getConfig)
let installPath = mkBuildPrefix buildDir config "android"
</> abi
</> takeFileName libmethcla
installPath %> copyFile' libmethcla
let testInstallPath = "tests/android/libs" </> abi </> takeFileName libmethcla_tests
testInstallPath %> copyFile' libmethcla_tests
return (installPath, testInstallPath)
phony "android" $ need $ map fst libs
phony "android-tests" $ need $ map snd libs
-- Pepper/PNaCl
do
let getConfig = getConfigFromWithEnv [] "config/pepper.cfg" []
target = NaCl.target
naclConfig = case config of
Debug -> NaCl.Debug
Release -> NaCl.Release
toolChain = NaCl.toolChain
<$> getEnv' "NACL_SDK"
<*> (maybe (error "PEPPER_SDK_VERSION undefined")
(maybe (error "Couldn't parse PEPPER_SDK_VERSION")
NaCl.pepper . readMaybe)
<$> getConfig "PEPPER_SDK_VERSION")
<*> pure naclConfig
<*> pure target
buildPrefix = targetBuildPrefix' target
libmethcla <- staticLibrary toolChain
(targetBuildPrefix' target </> "libmethcla.a")
(getBuildFlags getConfig)
(getSources getConfig)
phony "pepper" $ need [libmethcla]
let getConfigTests = getConfigFromWithEnv [] "config/pepper_tests.cfg" []
pnacl_test_bc <- executable toolChain
(buildPrefix </> "methcla-pnacl-tests.bc")
(getBuildFlags getConfigTests
>>>= pure (append localLibraries [libmethcla]))
(getSources getConfigTests)
let pnacl_test = (pnacl_test_bc `replaceExtension` "pexe")
pnacl_test %> \out -> join $ NaCl.finalize <$> toolChain <*> pure pnacl_test_bc <*> pure out
let pnacl_test_nmf = pnacl_test `replaceExtension` "nmf"
pnacl_test_nmf %> NaCl.mk_nmf (NaCl.Program (NaCl.Executable pnacl_test Nothing) Nothing)
let pnacl_test' = "tests/pnacl" </> takeFileName pnacl_test
pnacl_test' %> copyFile' pnacl_test
let pnacl_test_nmf' = "tests/pnacl" </> takeFileName pnacl_test_nmf
pnacl_test_nmf' %> copyFile' pnacl_test_nmf
phony "pnacl-test" $ need [pnacl_test', pnacl_test_nmf']
-- let examplesBuildFlags flags = do
-- libmethcla <- get_libmethcla
-- return $ buildFlags >>> libmethcla >>> flags
-- examplesDir = buildDir </> "examples/pnacl"
-- mkExample output flags sources files = do
-- let outputDir = takeDirectory output
-- bc <- executable toolChain (buildPrefix </> takeFileName output <.> "bc")
-- $ SourceTree.flagsM (examplesBuildFlags flags)
-- $ SourceTree.files sources
-- let pexe = bc `replaceExtension` "pexe"
-- `replaceDirectory` (outputDir </> show naclConfig)
-- pexe %> NaCl.finalize toolChain bc
-- let nmf = pexe `replaceExtension` "nmf"
-- nmf %> NaCl.mk_nmf [(NaCl.PNaCl, pexe)]
-- let allFiles = ["examples/common/common.js"] ++ files
-- allFiles' = map (`replaceDirectory` outputDir) allFiles
-- mapM_ (\(old, new) -> new %> copyFile' old) (zip allFiles allFiles')
--
-- return $ [pexe, nmf] ++ allFiles'
--
-- thaddeus <- mkExample ( examplesDir </> "thaddeus/methcla-thaddeus" )
-- ( append userIncludes [ "examples/thADDeus/src" ] )
-- [
-- "examples/thADDeus/pnacl/main.cpp"
-- , "examples/thADDeus/src/synth.cpp"
-- ]
-- [
-- "examples/thADDeus/pnacl/index.html"
-- , "examples/thADDeus/pnacl/manifest.json"
-- , "examples/thADDeus/pnacl/thaddeus.js"
-- ]
-- sampler <- mkExample ( examplesDir </> "sampler/methcla-sampler" )
-- ( append userIncludes [ "examples/sampler/src" ]
-- . append systemIncludes [ "examples/sampler/libs/tinydir" ]
-- . NaCl.libnacl_io )
-- [
-- "examples/sampler/pnacl/main.cpp"
-- , "examples/sampler/src/Engine.cpp"
-- ]
-- [
-- "examples/sampler/pnacl/index.html"
-- , "examples/sampler/pnacl/manifest.json"
-- , "examples/sampler/pnacl/example.js"
-- ]
--
-- let freesounds = map (takeFileName.takeDirectory) [
-- "http://freesound.org/people/adejabor/sounds/157965/"
-- , "http://freesound.org/people/NOISE.INC/sounds/45394/"
-- , "http://freesound.org/people/ThePriest909/sounds/209331/"
-- ]
--
-- (examplesDir </> "sampler/sounds/*") %> \output -> do
-- freesoundApiKey <- getEnv'
-- "FREESOUND_API_KEY"
-- cmd "curl" [ "http://www.freesound.org/api/sounds/"
-- ++ dropExtension (takeFileName output)
-- ++ "/serve?api_key=" ++ freesoundApiKey
-- , "-L", "-o", output ] :: Action ()
--
-- -- Install warp-static web server if needed
-- let server = ".cabal-sandbox/bin/warp"
--
-- server %> \_ -> do
-- cmd "cabal sandbox init" :: Action ()
-- cmd "cabal install -j warp-static" :: Action ()
--
-- phony "pnacl-examples" $ do
-- need [server]
-- need thaddeus
-- need $ sampler ++ map (combine (examplesDir </> "sampler/sounds"))
-- freesounds
-- cmd (Cwd examplesDir) server :: Action ()
-- Desktop host targets
do
let (target, toolChain) = second ((=<<) applyEnv) Host.defaultToolChain
hostToolChainConfig = buildDir </> "config/host_toolchain.cfg"
hostToolChainConfig %> \out -> do
alwaysRerun
tc <- toolChain
let tcVariant = case get toolChainVariant options of
ToolChain.Generic -> get ToolChain.variant tc
v -> v
writeFileChanged out $ unlines [
"ToolChain.variant = " ++ map toLower (show tcVariant)
]
-- Library for host operating system
do
let getConfig = getConfigFromWithEnv [hostToolChainConfig] "config/host_library.cfg" [("Target.os", map toLower . show . targetOS $ target)]
build arch f ext =
f toolChain (targetBuildPrefix' (maybe target (\a -> target { targetArch = a}) arch) </> "libmethcla" <.> ext)
(bf arch <$> getBuildFlags getConfig)
(getSources getConfig)
where
bf arch =
case arch of
Just (X86 I686) -> (>>> append compilerFlags [(Nothing, ["-m32"])] >>> append linkerFlags ["-m32"])
Just (X86 X86_64) -> (>>> append compilerFlags [(Nothing, ["-m64"])] >>> append linkerFlags ["-m64"])
_ -> id
case targetOS target of
os | os == Linux || os == Windows -> do
case targetArch target of
X86 a -> do
staticLib32 <- build (Just (X86 I686)) staticLibrary "a"
sharedLib32 <- build (Just (X86 I686)) sharedLibrary Host.sharedLibraryExtension
staticLib64 <- build (Just (X86 X86_64)) staticLibrary "a"
sharedLib64 <- build (Just (X86 X86_64)) sharedLibrary Host.sharedLibraryExtension
phony "desktop32" $ need [staticLib32, sharedLib32]
phony "desktop64" $ need [staticLib64, sharedLib64]
case a of
I386 -> phony "desktop" $ need ["desktop32"]
I686 -> phony "desktop" $ need ["desktop32"]
X86_64 -> phony "desktop" $ need ["desktop64"]
_ -> do
staticLib <- build Nothing staticLibrary "a"
sharedLib <- build Nothing sharedLibrary Host.sharedLibraryExtension
phony "desktop" $ need [staticLib, sharedLib]
_ -> do
staticLib <- build Nothing staticLibrary "a"
sharedLib <- build Nothing sharedLibrary Host.sharedLibraryExtension
phony "desktop" $ need [staticLib, sharedLib]
-- Tests for host operating system
do
let getConfig = getConfigFromWithEnv [hostToolChainConfig] "config/host_tests.cfg" [("Target.os", map toLower . show . targetOS $ target)]
result <- executable toolChain
(targetBuildPrefix' target </> "methcla-tests" <.> Host.executableExtension)
(getBuildFlags getConfig)
(getSources getConfig)
phony "host-tests" $ need [result]
phony "test" $ do
need [result]
command_ [] result []
phony "clean-test" $ removeFilesAfter "tests/output" ["*.osc", "*.wav"]
-- tags
"tags" %> \tags ->
cmd "ctags -R --languages=c,c++ --sort=foldcase --c++-kinds=+p --fields=+iaS --extra=+qf --tag-relative=yes"
"--exclude='*typeof*'"
"-f" tags
"external_libraries"
"include"
"platform"
"plugins"
"src"