|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (C) 2006-2017 wolfSSL Inc. |
| 5 | +# |
| 6 | +# This file is part of wolfSSL. (formerly known as CyaSSL) |
| 7 | +# |
| 8 | +# wolfSSL is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation; either version 2 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# wolfSSL is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program; if not, write to the Free Software |
| 20 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 21 | + |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | +from contextlib import contextmanager |
| 25 | +from distutils.util import get_platform |
| 26 | + |
| 27 | + |
| 28 | +def local_path(path): |
| 29 | + """ Return path relative to the root of this project |
| 30 | + """ |
| 31 | + current = os.path.dirname(__file__) |
| 32 | + gparent = os.path.dirname(os.path.dirname(current)) |
| 33 | + return os.path.abspath(os.path.join(gparent, path)) |
| 34 | + |
| 35 | + |
| 36 | +WOLFSSL_GIT_ADDR = "https://github.com/wolfssl/wolfssl.git" |
| 37 | +WOLFSSL_SRC_PATH = local_path("lib/wolfssl/src") |
| 38 | + |
| 39 | + |
| 40 | +def call(cmd): |
| 41 | + print("Calling: '{}' from working directory {}".format(cmd, os.getcwd())) |
| 42 | + |
| 43 | + old_env = os.environ["PATH"] |
| 44 | + os.environ["PATH"] = "{}:{}".format(WOLFSSL_SRC_PATH, old_env) |
| 45 | + subprocess.check_call(cmd, shell=True, env=os.environ) |
| 46 | + os.environ["PATH"] = old_env |
| 47 | + |
| 48 | + |
| 49 | +@contextmanager |
| 50 | +def chdir(new_path, mkdir=False): |
| 51 | + old_path = os.getcwd() |
| 52 | + |
| 53 | + if mkdir: |
| 54 | + try: |
| 55 | + os.mkdir(new_path) |
| 56 | + except OSError: |
| 57 | + pass |
| 58 | + |
| 59 | + try: |
| 60 | + yield os.chdir(new_path) |
| 61 | + finally: |
| 62 | + os.chdir(old_path) |
| 63 | + |
| 64 | + |
| 65 | +def clone_wolfssl(version): |
| 66 | + """ Clone wolfSSL C library repository |
| 67 | + """ |
| 68 | + call("git clone --depth=1 --branch={} {} {}".format( |
| 69 | + version, WOLFSSL_GIT_ADDR, WOLFSSL_SRC_PATH)) |
| 70 | + |
| 71 | + |
| 72 | +def checkout_version(version): |
| 73 | + """ Ensure that we have the right version |
| 74 | + """ |
| 75 | + with chdir(WOLFSSL_SRC_PATH): |
| 76 | + current = subprocess.check_output( |
| 77 | + ["git", "describe", "--all", "--exact-match"] |
| 78 | + ).strip().decode().split('/')[-1] |
| 79 | + |
| 80 | + if current != version: |
| 81 | + tags = subprocess.check_output( |
| 82 | + ["git", "tag"] |
| 83 | + ).strip().decode().split("\n") |
| 84 | + |
| 85 | + if version != "master" and version not in tags: |
| 86 | + call("git fetch --depth=1 origin tag {}".format(version)) |
| 87 | + |
| 88 | + call("git checkout --force {}".format(version)) |
| 89 | + |
| 90 | + return True # rebuild needed |
| 91 | + |
| 92 | + return False |
| 93 | + |
| 94 | + |
| 95 | +def ensure_wolfssl_src(version): |
| 96 | + """ Ensure that wolfssl sources are presents and up-to-date |
| 97 | + """ |
| 98 | + if not os.path.isdir(WOLFSSL_SRC_PATH): |
| 99 | + clone_wolfssl(version) |
| 100 | + return True |
| 101 | + |
| 102 | + return checkout_version(version) |
| 103 | + |
| 104 | + |
| 105 | +def make_flags(prefix): |
| 106 | + """ Returns compilation flags |
| 107 | + """ |
| 108 | + flags = [] |
| 109 | + |
| 110 | + if get_platform() in ["linux-x86_64", "linux-i686"]: |
| 111 | + flags.append("CFLAGS=-fpic") |
| 112 | + |
| 113 | + # install location |
| 114 | + flags.append("--prefix={}".format(prefix)) |
| 115 | + |
| 116 | + # lib only |
| 117 | + flags.append("--disable-shared") |
| 118 | + |
| 119 | + return " ".join(flags) |
| 120 | + |
| 121 | + |
| 122 | +def make(configure_flags): |
| 123 | + """ Create a release of wolfSSL C library |
| 124 | + """ |
| 125 | + with chdir(WOLFSSL_SRC_PATH): |
| 126 | + call("git clean -fdX") |
| 127 | + |
| 128 | + try: |
| 129 | + call("./autogen.sh") |
| 130 | + except subprocess.CalledProcessError: |
| 131 | + call("libtoolize") |
| 132 | + call("./autogen.sh") |
| 133 | + |
| 134 | + call("./configure {}".format(configure_flags)) |
| 135 | + call("make") |
| 136 | + call("make install-exec") |
| 137 | + |
| 138 | + |
| 139 | +def build_wolfssl(version="master"): |
| 140 | + prefix = local_path("lib/wolfssl/{}/{}".format( |
| 141 | + get_platform(), version)) |
| 142 | + libfile = os.path.join(prefix, 'lib/libwolfssl.la') |
| 143 | + |
| 144 | + rebuild = ensure_wolfssl_src(version) |
| 145 | + |
| 146 | + if rebuild or not os.path.isfile(libfile): |
| 147 | + make(make_flags(prefix)) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + build_wolfssl() |
0 commit comments