summaryrefslogtreecommitdiff
path: root/src/couch/rebar.config.script
blob: bd35e34bd541433c8e179f507d06109d84bd7246 (plain)
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
%% 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.

CopyIfDifferent = fun(Path, Contents) ->
    case filelib:is_file(Path) of
        true ->
            case file:read_file(Path) of
                {ok, Contents} ->
                    ok;
                _ ->
                    file:write_file(Path, Contents)
            end;
        false ->
            file:write_file(Path, Contents)
    end
end,


CouchJSName = case os:type() of
    {win32, _} ->
        "couchjs.exe";
    _ ->
        "couchjs"
end,
CouchJSPath = filename:join(["priv", CouchJSName]),
Version = case os:getenv("COUCHDB_VERSION") of
    false ->
        string:strip(os:cmd("git describe --always"), right, $\n);
    Version0 ->
        string:strip(Version0, right)
end,

CouchConfig = case filelib:is_file(os:getenv("COUCHDB_CONFIG")) of
    true ->
        {ok, Result} = file:consult(os:getenv("COUCHDB_CONFIG")),
        Result;
    false ->
        []
end.

ConfigH = [
    {"SM185", ""},
    {"HAVE_JS_GET_STRING_CHARS_AND_LENGTH", "1"},
    {"JSSCRIPT_TYPE", "JSObject*"},
    {"COUCHJS_NAME", "\"" ++ CouchJSName++ "\""},
    {"PACKAGE", "\"apache-couchdb\""},
    {"PACKAGE_BUGREPORT", "\"https://issues.apache.org/jira/browse/COUCHDB\""},
    {"PACKAGE_NAME", "\"Apache CouchDB\""},
    {"PACKAGE_STRING", "\"Apache CouchDB " ++ Version ++ "\""},
    {"PACKAGE_VERSION", "\"" ++ Version ++ "\""}
],

CouchJSConfig = "priv/couch_js/config.h",
ConfigSrc = [["#define ", K, " ", V, $\n] || {K, V} <- ConfigH],
ConfigBin = iolist_to_binary(ConfigSrc),
ok = CopyIfDifferent(CouchJSConfig, ConfigBin),

%% TODO support curl on Windows
{JS_CFLAGS, JS_LDFLAGS} = case lists:keyfind(with_curl, 1, CouchConfig) of
    {with_curl, true} ->
        case os:type() of
            {win32, _} ->
                {"/DHAVE_CURL /IC:\\relax\\curl\\include", "/LIBPATH:C:\\relax\\js-1.8.5\\js\\src /LIBPATH:C:\\Relax\\curl\\lib\\release-ssl mozjs185-1.0.lib libcurl_imp.lib"};
            {unix, freebsd} ->
                {"-DHAVE_CURL -I/usr/local/include", "-DHAVE_CURL -lmozjs185 -lcurl"};
            _ ->
                {"-DHAVE_CURL", "-DHAVE_CURL -lmozjs185 -lcurl"}
        end;
    _ ->
        case os:type() of
            {win32, _} ->
                {"", "/LIBPATH:C:\\relax\\js-1.8.5\\js\\src mozjs185-1.0.lib"};
            _ ->
                {"", "-lmozjs185"}
        end
end,

CouchJSSrc = ["priv/couch_js/*.c"],

IcuPath = "priv/couch_icu_driver.so",
IcuSrc = ["priv/icu_driver/*.c"],
IcuEnv = [{"DRV_CFLAGS",  "$DRV_CFLAGS -DPIC -O2 -fno-common"},
          {"DRV_LDFLAGS", "$DRV_LDFLAGS -lm -licuuc -licudata -licui18n -lpthread"}],
IcuDarwinEnv = [{"CFLAGS", "-DXP_UNIX -I/usr/local/opt/icu4c/include"},
                {"LDFLAGS", "-L/usr/local/opt/icu4c/lib"}],
IcuBsdEnv = [{"CFLAGS", "-DXP_UNIX -I/usr/local/include"},
             {"LDFLAGS", "-L/usr/local/lib"}],
IcuWinEnv = [{"CFLAGS", "/DXP_WIN /IC:\\relax\\icu\\include"},
             {"LDFLAGS", "/LIBPATH:C:\\relax\\icu\\lib64 icuin.lib icudt.lib icuuc.lib"}],

ComparePath = "priv/couch_ejson_compare.so",
CompareSrc = ["priv/couch_ejson_compare/*.c"],

BaseSpecs = [
        %% couchjs
        {"darwin", CouchJSPath, CouchJSSrc, [{env, [{"CFLAGS", JS_CFLAGS ++ " -DXP_UNIX -I/usr/local/include/js"}, {"LDFLAGS", JS_LDFLAGS ++ " -L/usr/local/lib"}]}]},
        {"linux",  CouchJSPath, CouchJSSrc, [{env, [{"CFLAGS", JS_CFLAGS ++ " -DXP_UNIX -I/usr/include/js"}, {"LDFLAGS", JS_LDFLAGS ++ " -lm"}]}]},
        {"bsd",   CouchJSPath, CouchJSSrc, [{env, [{"CFLAGS", JS_CFLAGS ++ " -DXP_UNIX -I/usr/local/include/js"}, {"LDFLAGS", JS_LDFLAGS ++ " -L/usr/local/lib -lm"}]}]},
        {"win32",  CouchJSPath, CouchJSSrc, [{env, [{"CFLAGS", JS_CFLAGS ++ " /DXP_WIN /IC:\\relax\\js-1.8.5\\js\\src"}, {"LDFLAGS", JS_LDFLAGS}]}]},
        % ICU
        {"darwin", IcuPath, IcuSrc, [{env, IcuEnv ++ IcuDarwinEnv}]},
        {"linux",  IcuPath, IcuSrc, [{env, IcuEnv}]},
        {"bsd",   IcuPath, IcuSrc, [{env, IcuEnv ++ IcuBsdEnv}]},
        {"win32",  IcuPath, IcuSrc, [{env, IcuWinEnv}]},
        % ejson_compare
        {"darwin", ComparePath, CompareSrc, [{env, IcuEnv ++ IcuDarwinEnv}]},
        {"linux",  ComparePath, CompareSrc, [{env, IcuEnv}]},
        {"bsd",   ComparePath, CompareSrc, [{env, IcuEnv ++ IcuBsdEnv}]},
        {"win32",  ComparePath, CompareSrc, [{env, IcuWinEnv}]}
],

SpawnSpec = [
    {"priv/couchspawnkillable", ["priv/spawnkillable/*.c"]}
],

PortSpecs = case os:type() of
    {win32, _} ->
        BaseSpecs ++ SpawnSpec;
    _ ->
        {ok, CSK} = file:read_file("priv/spawnkillable/couchspawnkillable.sh"),
        ok = CopyIfDifferent("priv/couchspawnkillable", CSK),
        os:cmd("chmod +x priv/couchspawnkillable"),
        BaseSpecs
end,
PlatformDefines = [
   {platform_define, "^R16", 'NORANDMODULE'},
   {platform_define, "^17", 'NORANDMODULE'},
   {platform_define, "win32", 'WINDOWS'}
],
AddConfig = [
    {port_specs, PortSpecs},
    {erl_opts, PlatformDefines ++ [
        {d, 'COUCHDB_VERSION', Version},
        {i, "../"}
    ]},
    {eunit_compile_opts, PlatformDefines}
].

lists:foldl(fun({K, V}, CfgAcc) ->
    lists:keystore(K, 1, CfgAcc, {K, V})
end, CONFIG, AddConfig).