File size: 7,044 Bytes
83418c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys, os.path

def rsplit(toSplit, sub, max=-1):
    """ str.rsplit seems to have been introduced in 2.4 :( """
    l = []
    i = 0
    while i != max:
        try: idx = toSplit.rindex(sub)
        except ValueError: break

        toSplit, splitOff = toSplit[:idx], toSplit[idx + len(sub):]
        l.insert(0, splitOff)
        i += 1

    l.insert(0, toSplit)
    return l

sconsDir = os.path.join("build", "scons")
SConscript(os.path.join(sconsDir, "SConscript_common"))
Import("Platform", "Posix", "ApiVer")

# SConscript_opts exports PortAudio options
optsDict = SConscript(os.path.join(sconsDir, "SConscript_opts"))
optionsCache = os.path.join(sconsDir, "options.cache")   # Save options between runs in this cache
options = Options(optionsCache, args=ARGUMENTS)
for k in ("Installation Dirs", "Build Targets", "Host APIs", "Build Parameters", "Bindings"):
    options.AddOptions(*optsDict[k])
# Propagate options into environment
env = Environment(options=options)
# Save options for next run
options.Save(optionsCache, env)
# Generate help text for options
env.Help(options.GenerateHelpText(env))

buildDir = os.path.join("#", sconsDir, env["PLATFORM"])

# Determine parameters to build tools
if Platform in Posix:
    threadCFlags = ''
    if Platform != 'darwin':
        threadCFlags = "-pthread "
    baseLinkFlags = threadCFlags
    baseCxxFlags = baseCFlags = "-Wall -pedantic -pipe " + threadCFlags
    debugCxxFlags = debugCFlags = "-g"
    optCxxFlags = optCFlags  = "-O2"
env.Append(CCFLAGS = baseCFlags)
env.Append(CXXFLAGS = baseCxxFlags)
env.Append(LINKFLAGS = baseLinkFlags)
if env["enableDebug"]:
    env.AppendUnique(CCFLAGS=debugCFlags.split())
    env.AppendUnique(CXXFLAGS=debugCxxFlags.split())
if env["enableOptimize"]:
    env.AppendUnique(CCFLAGS=optCFlags.split())
    env.AppendUnique(CXXFLAGS=optCxxFlags.split())
if not env["enableAsserts"]:
    env.AppendUnique(CPPDEFINES=["-DNDEBUG"])
if env["customCFlags"]:
    env.Append(CCFLAGS=Split(env["customCFlags"]))
if env["customCxxFlags"]:
    env.Append(CXXFLAGS=Split(env["customCxxFlags"]))
if env["customLinkFlags"]:
    env.Append(LINKFLAGS=Split(env["customLinkFlags"]))

env.Append(CPPPATH=[os.path.join("#", "include"), "common"])

# Store all signatures in one file, otherwise .sconsign files will get installed along with our own files
env.SConsignFile(os.path.join(sconsDir, ".sconsign"))

env.SConscriptChdir(False)
sources, sharedLib, staticLib, tests, portEnv, hostApis = env.SConscript(os.path.join("src", "SConscript"),
        build_dir=buildDir, duplicate=False, exports=["env"])

if Platform in Posix:
    prefix = env["prefix"]
    includeDir = os.path.join(prefix, "include")
    libDir = os.path.join(prefix, "lib")
    env.Alias("install", includeDir)
    env.Alias("install", libDir)

    # pkg-config

    def installPkgconfig(env, target, source):
        tgt = str(target[0])
        src = str(source[0])
        f = open(src)
        try: txt = f.read()
        finally: f.close()
        txt = txt.replace("@prefix@", prefix)
        txt = txt.replace("@exec_prefix@", prefix)
        txt = txt.replace("@libdir@", libDir)
        txt = txt.replace("@includedir@", includeDir)
        txt = txt.replace("@LIBS@", " ".join(["-l%s" % l for l in portEnv["LIBS"]]))
        txt = txt.replace("@THREAD_CFLAGS@", threadCFlags)

        f = open(tgt, "w")
        try: f.write(txt)
        finally: f.close()

    pkgconfigTgt = "portaudio-%d.0.pc" % int(ApiVer.split(".", 1)[0])
    env.Command(os.path.join(libDir, "pkgconfig", pkgconfigTgt),
        os.path.join("#", pkgconfigTgt + ".in"), installPkgconfig)

# Default to None, since if the user disables all targets and no Default is set, all targets
# are built by default
env.Default(None)
if env["enableTests"]:
    env.Default(tests)
if env["enableShared"]:
    env.Default(sharedLib)

    if Platform in Posix:
        def symlink(env, target, source):
            trgt = str(target[0])
            src = str(source[0])

            if os.path.islink(trgt) or os.path.exists(trgt):
                os.remove(trgt)
            os.symlink(os.path.basename(src), trgt)

        major, minor, micro = [int(c) for c in ApiVer.split(".")]
        
        soFile = "%s.%s" % (os.path.basename(str(sharedLib[0])), ApiVer)
        env.InstallAs(target=os.path.join(libDir, soFile), source=sharedLib)
        # Install symlinks
        symTrgt = os.path.join(libDir, soFile)
        env.Command(os.path.join(libDir, "libportaudio.so.%d.%d" % (major, minor)),
            symTrgt, symlink)
        symTrgt = rsplit(symTrgt, ".", 1)[0]
        env.Command(os.path.join(libDir, "libportaudio.so.%d" % major), symTrgt, symlink)
        symTrgt = rsplit(symTrgt, ".", 1)[0]
        env.Command(os.path.join(libDir, "libportaudio.so"), symTrgt, symlink)

if env["enableStatic"]:
    env.Default(staticLib)
    env.Install(libDir, staticLib)

env.Install(includeDir, os.path.join("include", "portaudio.h"))


if env["enableCxx"]:
    env.SConscriptChdir(True)
    cxxEnv = env.Copy()
    sharedLibs, staticLibs, headers = env.SConscript(os.path.join("bindings", "cpp", "SConscript"),
            exports={"env": cxxEnv, "buildDir": buildDir}, build_dir=os.path.join(buildDir, "portaudiocpp"), duplicate=False)
    if env["enableStatic"]:
        env.Default(staticLibs)
        env.Install(libDir, staticLibs)
    if env["enableShared"]:
        env.Default(sharedLibs)
        env.Install(libDir, sharedLibs)
    env.Install(os.path.join(includeDir, "portaudiocpp"), headers)

# Generate portaudio_config.h header with compile-time definitions of which PA
# back-ends are available, and which includes back-end extension headers

# Host-specific headers
hostApiHeaders = {"ALSA": "pa_linux_alsa.h",
                    "ASIO": "pa_asio.h",
                    "COREAUDIO": "pa_mac_core.h",
                    "JACK": "pa_jack.h",
                    "WMME": "pa_winwmme.h",
                    }

def buildConfigH(target, source, env):
    """builder for portaudio_config.h"""
    global hostApiHeaders, hostApis
    out = ""
    for hostApi in hostApis:
        out += "#define PA_HAVE_%s\n" % hostApi

        hostApiSpecificHeader = hostApiHeaders.get(hostApi, None)
        if hostApiSpecificHeader:
            out += "#include \"%s\"\n" % hostApiSpecificHeader

        out += "\n"
    # Strip the last newline
    if out and out[-1] == "\n":
        out = out[:-1]

    f = file(str(target[0]), 'w')
    try: f.write(out)
    finally: f.close()
    return 0

# Define the builder for the config header
env.Append(BUILDERS={"portaudioConfig": env.Builder(
            action=Action(buildConfigH), target_factory=env.fs.File)})

confH = env.portaudioConfig(File("portaudio_config.h", "include"),
        File("portaudio.h", "include"))
env.Default(confH)
env.Install(os.path.join(includeDir, "portaudio"), confH)

for api in hostApis:
    if api in hostApiHeaders:
        env.Install(os.path.join(includeDir, "portaudio"),
                File(hostApiHeaders[api], "include"))