xacc-framework 5.31 KB
Newer Older
1
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
12
13
# *******************************************************************************
# Copyright (c) 2017 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
#   Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
14
15
16
17
import argparse
import sys
import os
import subprocess
Mccaskey, Alex's avatar
Mccaskey, Alex committed
18
import multiprocessing
19

Mccaskey, Alex's avatar
Mccaskey, Alex committed
20
21
22
23
24
25
try:
   import pyxacc as xacc
except ImportError:
   print('Error - cannot import XACC Python Bindings. Exiting')
   sys.exit()

26
def parse_args(args):
Mccaskey, Alex's avatar
Mccaskey, Alex committed
27
   parser = argparse.ArgumentParser(description="XACC Framework Utility.",
28
29
30
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                     fromfile_prefix_chars='@')
   parser.add_argument("-p", "--plugins", nargs='*', type=str, help="The XACC Plugins to install.", required=True)
31
   parser.add_argument("-a", "--extra-cmake-args", nargs='*', type=str, help="Any extra CMake arguments to drive the install.", required=False)
32
   parser.add_argument("-j", "--make-threads", default=4, type=int, help="Number of threads to use in make", required=False)
33
34
35
36
37
38
39
40
41
42
43
44
45
   opts = parser.parse_args(args)
   return opts

ornlqci = 'https://github.com/ornl-qci'
availablePluginUrls = { 'xacc-scaffold' : ornlqci+'/xacc-scaffold',
                    'xacc-rigetti' : ornlqci+'/xacc-rigetti',
                    'xacc-dwave' : ornlqci+'/xacc-dwave',
                    'xacc-ibm' : ornlqci+'/xacc-ibm',
                    'xacc-python' : ornlqci+'/xacc-python', 
                    'tnqvm' : ornlqci+'/tnqvm',
                    'ibm' : ornlqci+'/xacc-ibm',
                    'rigetti' : ornlqci+'/xacc-rigetti',
                    'scaffold' : ornlqci+'/xacc-scaffold',
46
47
                    'dwave' : ornlqci+'/xacc-dwave', 
		    'vqe' : ornlqci+'/xacc-vqe', 
48
49
		    'xacc-dwsapi-embedding' : ornlqci+'/xacc-dwsapi-embedding', 
                    'xacc-projectq' : ornlqci+'/xacc-projectq' }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

def mkdir_p(path):
    """ Operates like mkdir -p in a Unix-like system """
    try:
        os.makedirs(path)
    except OSError as e:
        if os.path.exists(path) and os.path.isdir(path):
            pass
        else:
            print("")
            print("--------------------------- ERROR -----------------------------")
            print("Cannot create directory " + path)
            print("--------------------------- ERROR -----------------------------")
            print("")
            exit()

def main(argv=None):
   opts = parse_args(sys.argv[1:])

Mccaskey, Alex's avatar
Mccaskey, Alex committed
69
70
71
72
73
   # pyxacc can be installed in site-packages/xacc-...-egg or 
   # in ${XACC_DIR}/lib/python, we need to figure out which one
   xaccLocation = os.path.dirname(os.path.realpath(xacc.__file__))
   if os.path.basename(xaccLocation) == 'python':
      xaccLocation = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
74
   
Mccaskey, Alex's avatar
Mccaskey, Alex committed
75
   print('XACC Install Location = ', xaccLocation)
Mccaskey, Alex's avatar
Mccaskey, Alex committed
76
   xacc_cwd = os.getcwd()
77
   
Mccaskey, Alex's avatar
Mccaskey, Alex committed
78
   cpus = str(multiprocessing.cpu_count())
79
80
   if not opts.make_threads == None: 
      cpus = str(opts.make_threads)
Mccaskey, Alex's avatar
Mccaskey, Alex committed
81

82
83
   # Get the plugins we're supposed to install
   plugins = opts.plugins
84
85
86
87
88
   cmake_args = ''

   if not opts.extra_cmake_args == None:
      for arg in opts.extra_cmake_args:
         cmake_args += '-D'+ arg + ' '
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

   # Loop over the plugins and install them
   for plugin in plugins:
      if plugin not in availablePluginUrls:
         print("")
         print("--------------------------- ERROR -----------------------------")
         print("Invalid plugin name - " + plugin)
         print("--------------------------- ERROR -----------------------------")
         print("")
         exit()
         
      # Create a CMakeLists.txt file
      cmakeContents = """
      project(""" + plugin + """-project LANGUAGES CXX)
      cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
      set(CMAKE_STANDARD_REQUIRED ON)
      set(CMAKE_CXX_STANDARD 14)
      set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
      set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
      include(ExternalProject)
      ExternalProject_Add("""+plugin+"""
                 GIT_REPOSITORY """+availablePluginUrls[plugin]+"""
                 GIT_TAG master
112
                 CMAKE_ARGS -DXACC_DIR="""+xaccLocation+' '+cmake_args+"""
113
                 BUILD_ALWAYS 1
Mccaskey, Alex's avatar
Mccaskey, Alex committed
114
                 INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} install
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
              )
          """
      # Create a build directory
      mkdir_p(plugin+'-install')
      mkdir_p(plugin+'-install/build')
      os.chdir(plugin+'-install')

      # Write the CMakeLists file 
      cmakelists = open("CMakeLists.txt", "w")
      cmakelists.write("%s" % cmakeContents)
      cmakelists.close()
      
      # Execute the build
      os.chdir('build')
      cmakecmd = ['cmake', '..']
      subprocess.check_call(cmakecmd, stderr=subprocess.STDOUT, shell=False)
Mccaskey, Alex's avatar
Mccaskey, Alex committed
131
      subprocess.check_call(['make', '-j'+cpus], stderr=subprocess.STDOUT, shell=False)
Mccaskey, Alex's avatar
Mccaskey, Alex committed
132
133
      
      os.chdir(xacc_cwd)
134
135
136

if __name__ == "__main__":
    sys.exit(main())