def build_drivers()

in buildscripts/buildtools.py [0:0]


    def build_drivers(self, make_clean = False, dest = None, log_file = None):
        """Build sqlsrv/pdo_sqlsrv extensions for PHP, assuming the Source folder 
        exists in the working directory, and this folder will be removed when the build 
        is complete.
        """
        work_dir = os.path.dirname(os.path.realpath(__file__))   
        # First, update the driver source file contents
        source_dir = os.path.join(work_dir, 'Source')
        if self.driver == 'all':
            self.update_driver_source(source_dir, 'sqlsrv') 
            self.update_driver_source(source_dir, 'pdo_sqlsrv') 
        else:
            self.update_driver_source(source_dir, self.driver) 

        # Next, generate the build configuration and arguments
        cmd_line = self.generate_build_options()
        print('cmd_line: ' + cmd_line)

        # Generate a batch file based on the inputs
        if log_file is None:
            log_file = self.get_logfile_name()
        
        batch_file = self.create_local_batch_file(make_clean, cmd_line, log_file)
        
        # Reference: https://github.com/OSTC/php-sdk-binary-tools
        # Clone the master branch of PHP sdk if the directory does not exist 
        print('Downloading the latest php SDK...')
        
        # if *dest* is None, simply use the current working directory
        sdk_dir = dest
        copy_to_ext = True      # this determines where to copy the binaries to
        if dest is None:
            sdk_dir = work_dir
            copy_to_ext = False

        phpSDK = os.path.join(sdk_dir, 'php-sdk')
        if not os.path.exists( phpSDK ):
            os.system('git clone https://github.com/OSTC/php-sdk-binary-tools.git --branch master --single-branch --depth 1 ' + phpSDK)
        os.chdir(phpSDK)
        os.system('git pull ')
        print('Done cloning the latest php SDK...')

        # Move the generated batch file to phpSDK for the php starter script 
        print('Moving the sdk bath file over...')
        sdk_batch_file = os.path.join(phpSDK, batch_file)
        if os.path.exists(sdk_batch_file):
            os.remove(sdk_batch_file)
        shutil.move(os.path.join(work_dir, batch_file), phpSDK)
        
        print('Checking if source exists...')
        sdk_source = os.path.join(phpSDK, 'Source')
        # Sometimes, for various reasons, the Source folder from previous build 
        # might exist in phpSDK. If so, remove it first
        if os.path.exists(sdk_source):  
            os.chmod(sdk_source, stat.S_IWRITE)
            shutil.rmtree(sdk_source, ignore_errors=True) 
        shutil.move(source_dir, phpSDK)
        
        # Invoke phpsdk-<vc>-<arch>.bat
        vc = self.compiler_version(sdk_dir)
        starter_script = 'phpsdk-' + vc + '-' + self.arch + '.bat'
        print('Running starter script: ', starter_script)
        os.system(starter_script + ' -t ' + batch_file)
        
        # Now we can safely remove the Source folder, because its contents have 
        # already been modified prior to building the extensions
        shutil.rmtree(os.path.join(phpSDK, 'Source'), ignore_errors=True) 
        
        # Next, rename the newly compiled PHP extensions, if required
        if not self.no_rename:
            self.rename_binaries(sdk_dir)
        
        # Final step, copy the binaries to the right place
        ext_dir = self.copy_binaries(sdk_dir, copy_to_ext)
        
        return ext_dir