aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/meta/meta-newsdk/sdktool.py
blob: 6c31e3e8666351d5a1f970f0f992222f388e6270 (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
#!/usr/bin/env python

# SDK tool
#
# Copyright (C) 2014 Intel Corporation
#
# Licensed under the MIT license, see COPYING.MIT for details

import sys
import os
import argparse

bitbake_subdir = 'layers/BITBAKE_LAYER/bitbake'
init_subdir = 'layers/BITBAKE_LAYER'
init_script = 'oe-init-build-env'

basepath = os.path.abspath(__file__)
if '/sysroots/' in basepath:
    basepath = basepath.split('/sysroots/')[0]


def read_workspace():
    workspace = {}
    with open(basepath + '/conf/work-config.inc', 'r') as f:
        for line in f:
            if line.startswith('EXTERNALSRC_'):
                splitval = line.split('=', 2)
                recipe = splitval[0].split('_pn-', 2)[1].rstrip()
                value = splitval[1].strip('" \n\r\t')
                workspace[recipe] = value
    return workspace

def create_recipe(pn):
    import bb
    recipedir = os.path.join(basepath, 'workspace', pn)
    bb.utils.mkdirhier(recipedir)
    # FIXME: Obviously this is very crude, but we don't have a means of creating a proper recipe automatically yet
    with open(os.path.join(recipedir, "%s.bb" % pn), 'w') as f:
        f.write('LICENSE = "CLOSED"\n')
        f.write('inherit autotools\n')

def add(args):
    workspace = read_workspace()
    if args.recipename in workspace:
        print("Error: recipe %s is already in your workspace" % args.recipename)
        return -1

    # FIXME we should probably do these as bbappends to avoid reparsing
    with open(basepath + '/conf/work-config.inc', 'a') as f:
        f.write('EXTERNALSRC_pn-%s = "%s"\n' % (args.recipename, args.srctree))

    create_recipe(args.recipename)

    return 0

def status(args):
    workspace = read_workspace()
    for recipe, value in workspace.iteritems():
        print("%s: %s" % (recipe, value))
    return 0

def build(args):
    import bb
    stdout, stderr = bb.process.run('. %s %s ; bitbake -c install -b %s.bb' % (os.path.join(basepath, init_subdir, init_script), basepath, args.recipename), cwd=basepath)
    print stdout
    print stderr

    return 0

def main():
    global basepath

    parser = argparse.ArgumentParser(description="SDK tool")
    parser.add_argument('--basepath', help='Base directory of SDK')

    subparsers = parser.add_subparsers()
    parser_add = subparsers.add_parser('add', help='Add a new recipe')
    parser_add.add_argument('recipename', help='Name for new recipe to add')
    parser_add.add_argument('srctree', help='Path to external source tree')
    parser_add.set_defaults(func=add)

    parser_status = subparsers.add_parser('status', help='Show status')
    parser_status.set_defaults(func=status)

    parser_build = subparsers.add_parser('build', help='Build recipe')
    parser_build.add_argument('recipename', help='Recipe to build')
    parser_build.set_defaults(func=build)

    args = parser.parse_args()

    if args.basepath:
        basepath = args.basepath

    if not os.path.exists(basepath + '/conf/work-config.inc'):
        print('Error: basepath %s is not valid' % basepath)
        return -1

    sys.path.insert(0, os.path.join(basepath, bitbake_subdir, 'lib'))

    ret = args.func(args)

    return ret


if __name__ == "__main__":
    try:
        ret = main()
    except Exception:
        ret = 1
        import traceback
        traceback.print_exc(5)
    sys.exit(ret)