support.go raw

   1  //go:build !purego
   2  
   3  //===- support.go - Bindings for support ----------------------------------===//
   4  //
   5  // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
   6  // See https://llvm.org/LICENSE.txt for license information.
   7  // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
   8  //
   9  //===----------------------------------------------------------------------===//
  10  //
  11  // This file defines bindings for the support component.
  12  //
  13  //===----------------------------------------------------------------------===//
  14  
  15  package llvm
  16  
  17  /*
  18  #include "llvm-c/Support.h"
  19  #include "SupportBindings.h"
  20  #include <stdlib.h>
  21  */
  22  import "C"
  23  
  24  import (
  25  	"errors"
  26  	"unsafe"
  27  )
  28  
  29  // Loads a dynamic library such that it may be used as an LLVM plugin.
  30  // See llvm::sys::DynamicLibrary::LoadLibraryPermanently.
  31  func LoadLibraryPermanently(lib string) error {
  32  	var errstr *C.char
  33  	libstr := C.CString(lib)
  34  	defer C.free(unsafe.Pointer(libstr))
  35  	C.LLVMLoadLibraryPermanently2(libstr, &errstr)
  36  	if errstr != nil {
  37  		err := errors.New(C.GoString(errstr))
  38  		C.free(unsafe.Pointer(errstr))
  39  		return err
  40  	}
  41  	return nil
  42  }
  43  
  44  // Parse the given arguments using the LLVM command line parser.
  45  // See llvm::cl::ParseCommandLineOptions.
  46  func ParseCommandLineOptions(args []string, overview string) {
  47  	argstrs := make([]*C.char, len(args))
  48  	for i, arg := range args {
  49  		argstrs[i] = C.CString(arg)
  50  		defer C.free(unsafe.Pointer(argstrs[i]))
  51  	}
  52  	overviewstr := C.CString(overview)
  53  	defer C.free(unsafe.Pointer(overviewstr))
  54  	C.LLVMParseCommandLineOptions(C.int(len(args)), &argstrs[0], overviewstr)
  55  }
  56