summaryrefslogtreecommitdiff
path: root/bin/eo_js_gen.py
blob: 7855ceb23b23deb8897e3c7ec7251eab15533c3e (plain) (blame)
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
#!/usr/bin/env python

from eoparser.helper import isXML, abs_path_get, dir_files_get, normalize_names
from eoparser.helper import _const
from eoparser.xmlparser import XMLparser
from argparse import ArgumentParser
import os, sys

const = _const()


def verbose_true(mes):
  print mes

def verbose_false(mes):
  pass

def makefile_file_generate(args, c_files, outdir):
   module_name = args.module
   pkg = args.pkg
   incl_paths = ["."]
   incl_paths += args.include_paths
   libs = args.libraries
   lib_paths = args.library_paths
   cpp_defines = args.cpp_defines

   #if someone wants to add -DSOME_STR=\"str\"
   #I will add one more escape symbol, because I also generate Makefile
   if cpp_defines is not None:   
     lst_tmp = []
     for d in cpp_defines:
       lst_tmp.append(d.replace("\"", "\\\""))
     cpp_defines = list(lst_tmp)
     del lst_tmp

   makefile_file = "Makefile"
   default_c_files = ['main.cc', 'CElmObject.cc', '_eobase.cc', '_module.cc']
   c_files = default_c_files + c_files

   lines = []
   lines.append("CC=g++")
   lines.append("all: lib%s.so"%module_name)
   lines.append("\n")
   lines.append("lib_SOURCES = \\")
   for f in c_files:
      lines.append("%s \\"%f)
   lines[-1] = lines[-1][:-2]

   lines.append("")
   lines.append("lib_OBJECTS = $(lib_SOURCES:.cc=.o)")

   lines.append("")
   lines.append("lib_CFLAGS = \\")
  # lines.append("-I. \\")
  #lines.append("-DPACKAGE_DATA_DIR=\\\"/tmp\\\" \\")
  #lines.append("-DPACKAGE_TMP_DIR=\\\"/tmp\\\" \\")

   for i in incl_paths:
     lines.append("-I%s \\"%i)

   for l in pkg.split():
     lines.append("`pkg-config --cflags %s` \\"%l)

   if cpp_defines is not None:   
     for d in cpp_defines:
       lines.append("-D%s \\"%d)
   lines[-1] = lines[-1][:-2]

   lines.append("")
   lines.append("lib_LDFLAGS = \\")
   for l in pkg.split():
      lines.append("`pkg-config --libs %s` \\"%l)

   if lib_paths is not None:
     for l in lib_paths:
       lines.append("-L%s \\"%l)

   if libs is not None:
     for l in libs:
       lines.append("-l%s \\"%l)
   lines[-1] = lines[-1][:-2]

   lines.append("")
   lines.append("lib%s.so: $(lib_OBJECTS)"%module_name)
   lines.append("\t$(CC) -shared -Wl,-soname,$@ -o $@ $^ $(lib_LDFLAGS)")

   lines.append("")
   lines.append("%.o : %.cc $(libjse_HEADERS)")
   lines.append("\t$(CC) -fPIC $(lib_CFLAGS) -c $< -o $@")

   lines.append("")
   lines.append("clean:")
   lines.append("\trm -f *~ *.o *.so")


   f = open(os.path.join(outdir, makefile_file), 'w')
   for l in lines:
     f.write(l + "\n")
   f.close()




def main():
  parser = ArgumentParser()
  parser.add_argument("-d", "--dir", dest="directory",
                  action="append", help="Source files")

  parser.add_argument("-o", "--outdir", dest="outdir",
                  action="store", help="Output directory")

  parser.add_argument("-v", "--verbose",
                  action="store_true", dest="verbose", default=False,
                  help="Print status messages to stdout. Default: False")

  parser.add_argument("-X", "--xmldir", dest="xmldir", default=sys.path,
                  action="append", help="Include eobase directory")

  parser.add_argument("--pkg", dest="pkg", default = "elementary eo",
        action="store", help="pkg-confing libraries. Default: \"elementary eo\"")

  parser.add_argument("-m", "--module", dest="module",
                  action="store", help="Name of module to generate")

  parser.add_argument("-I", "--include", dest="include_paths",
                  action="append", help="Pre-processor include path")

  parser.add_argument("-l", "--library", dest="libraries",
                  action="append", help="Libraries of this unit")

  parser.add_argument("-L", "--library-path", dest="library_paths",
                  action="append", help="Directories to search for libraries")

  parser.add_argument("-D", "--define", dest="cpp_defines",
                  action="append", help="Pre-processor define")


  args = parser.parse_args()

  verbose_print = verbose_true if args.verbose is True else verbose_false
  verbose_print("Options: %s"%args)

  directories = []
  outdir = ""
  sourcedir = ""
  incl_dirs = []

  if args.directory == None:
    print "ERROR: No source directory was provided"
    exit(1)
  elif args.outdir == None:
    print "ERROR: No out directory was provided"
    exit(1)
  elif args.module == None:
    print "ERROR: No module name was provided"
    exit(1)
  else:
    directories = abs_path_get(args.directory)
    outdir = abs_path_get([args.outdir])[0]
    if args.xmldir is not None:
      incl_dirs = abs_path_get(args.xmldir, False)

  verbose_print("Dirs: %s"%directories)
  verbose_print("Outdir: %s"%outdir)
  verbose_print("Include dirs: %s"%incl_dirs)

  xml_files = dir_files_get(directories, False)
  xml_files = filter(isXML, xml_files)
  verbose_print("In Files: %s"%xml_files)

  xp = XMLparser()
  xp.module_parse(args.module, xml_files, incl_dirs)
  xp.js_code_generate(outdir)

  c_files = []
  for n, o in xp.objects.items():
    c_files.append(o.V.c_file.name)
 
  makefile_file_generate(args, c_files, outdir)

  del xp

if __name__ == "__main__":
  main()