get_build_path.py 1.87 KB
Newer Older
Pixhawk's avatar
Pixhawk committed
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
#!/usr/bin/python
# Author: Lenna X. Peterson (github.com/lennax)
# Determines appropriate path for CMake
#  Looks for "CMakeLists.txt" and "src/"
#   ( BUILDFILE and SRCDIR in find_build_dir() )
#  Searches the following paths:
#   1 Path of call
#   2 Path where script is located
#   3 Path above 2 (parent directory)
#   4 Path above 3 (grandparent directory)

import os # for getcwd(), os.path

def get_build_path():

    build_dir=""

    ## Initialize search paths
    call_dir = os.getcwd()
    script_dir = os.path.dirname(os.path.abspath(__file__))
    script_mom = os.path.abspath(script_dir + os.sep + os.pardir)
    script_grandma = os.path.abspath(script_mom + os.sep + os.pardir)
    if script_mom == call_dir:
        script_mom = ""
    if script_grandma == call_dir:
        script_grandma = ""

    ## Define function to search for required components for build
    def find_build_dir(search_dir):
        BUILDFILE = "CMakeLists.txt"
        #SRCDIR = "src"
        os.chdir(search_dir)
        if os.path.isfile(BUILDFILE):
            return search_dir
        return False

    ## Class to emulate if temp = x 
    #      (checking equality of x while assigning it to temp)
    #      Borrowed from Alex Martelli
    class Holder(object):
        def set(self, value):
            self.value = value
            return value
        def get(self):
            return self.value

    temp = Holder()

    ## Search paths for build components
    if temp.set(find_build_dir(call_dir)): 
        build_dir = temp.get()
    elif temp.set(find_build_dir(script_dir)): 
        build_dir = temp.get()
    elif script_mom and temp.set(find_build_dir(script_mom)): 
        build_dir = temp.get()
    elif script_grandma and temp.set(find_build_dir(script_grandma)):
        build_dir = temp.get()
    else: 
        return 0
    
    #print "I go now. Good luck, everybody!"
    return build_dir