LLVMConfig.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 13 Sep 2015 07:27:07 +0100
changeset 36 51ac97a0e87d
parent 21 64c5f01be2b3
child 45 12a2f6470ef4
permissions -rw-r--r--
Fixed LLVMConfig to handle lying `llvm-config --libdir` On recent Debian Jessie builds from http://llvm.org/apt/jessie/ it seems that `llvm-config-3.8 --libdir` returns non-existent directory. To workaround this, check for returned libdir existence and if does not exists, perform some guesswork. It guesswork does not help, throw an error.

"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
"{ Package: 'jv:llvm_s' }"

"{ NameSpace: Smalltalk }"

Object subclass:#LLVMConfig
	instanceVariableNames:''
	classVariableNames:'LLVMConfigPath BinDir LibDir LibraryPath'
	poolDictionaries:''
	category:'LLVM-S-Internal'
!

!LLVMConfig class methodsFor:'documentation'!

copyright
"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
! !

!LLVMConfig class methodsFor:'initialization'!

initialize
    "Invoked at system start or when the class is dynamically loaded."

    LLVMConfigPath := nil.
    OperatingSystem isUNIXlike ifTrue:[ 
        #('-3.8' '-3.7' '') do:[:ver |
            | path |
            path := OperatingSystem pathOfCommand: 'llvm-config' , ver.
            path notNil ifTrue:[
                self llvmConfigPath: path.
                ^ self.
            ]
        ].
        ^ self
    ].
    self error: 'This operating system is not supported'.

    "Modified: / 13-09-2015 / 07:03:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

llvmConfigPath: aStringOrFilename
    "Set's the path to LLVM config which effectively
     defines which version of LLVM to use.
     Must be called before using any other LLVM API"

    | path |

    path := aStringOrFilename asFilename.
    path exists ifFalse:[ 
        self error: '`', path pathName ,'` does not exist'.
        ^ self.
    ].
    path isExecutable ifFalse:[ 
        self error: '`', path pathName ,'` does not executable'.
        ^ self.
    ].
    LLVMConfigPath := path.
    BinDir := LibDir := LibraryPath := nil.

    "Created: / 07-08-2015 / 13:31:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMConfig class methodsFor:'accessing'!

bindir
    "Returns result of `llvm-config --bindir` as Filename"

    BinDir isNil ifTrue:[ 
        LLVMConfigPath isNil ifTrue:[ 
            self error: 'No path to `llvm-config` set, use #llvmConfigPath:'
        ].
        BinDir := OperatingSystem getCommandOutputFrom: LLVMConfigPath asString, ' --bindir'
    ].
    ^ BinDir

    "
    LLVMConfig bindir
    "

    "Created: / 07-08-2015 / 13:29:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

libdir
    "Returns result of `llvm-config --libdir` as Filename"

    LibDir isNil ifTrue:[ 
        LLVMConfigPath isNil ifTrue:[ 
            self error: 'No path to `llvm-config` set, use #llvmConfigPath:'
        ].
        LibDir := OperatingSystem getCommandOutputFrom: LLVMConfigPath asString, ' --libdir'.
        LibDir asFilename exists ifFalse:[ 
            Logger warning: 'libdir reported by %1 does not exist (%2)' with: LLVMConfigPath with: LibDir.
            OperatingSystem isUNIXlike ifTrue:[ 
                "/ Assume Linux here
                ExternalAddress pointerSize == 8 ifTrue:[ 
                    "/ Pure guess...
                    LibDir := '/usr/lib/x86_64-linux-gnu'.
                ] ifFalse:[ 
                    LibDir := '/usr/lib/i386-linux-gnu'.
                ].
            ] ifFalse:[
                self error: 'Unsupported platform'.
            ]
        ].  
        LibDir asFilename exists ifFalse:[ 
            self error: 'Cannot find LLVM libdir'
        ].
    ].
    ^ LibDir

    "
    LibDir := nil.
    LLVMConfig libdir
    "

    "Created: / 07-08-2015 / 13:41:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 13-09-2015 / 06:59:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

libraryPath
    "Return path to `libLLVM.x.y.z.so` for linking"

    LibraryPath isNil ifTrue:[ 
        | vsn libpath |             
        LLVMConfigPath isNil ifTrue:[ 
            self error: 'No path to `llvm-config` set, use #llvmConfigPath:'
        ].
        vsn := OperatingSystem getCommandOutputFrom: LLVMConfigPath asString, ' --version'.
        libpath := self libdir asFilename / ('libLLVM-', vsn , '.so').
        libpath exists ifFalse:[
            "/ OK, try libLLVM-x.y.so.1
            libpath := self libdir asFilename / ('libLLVM-', (vsn copyTo: (vsn lastIndexOf: $.) - 1)   , '.so.1').
            libpath exists ifFalse:[
                self error: 'Could not find libLLVM.x.y.z.so'
            ].
        ].
        LibraryPath := libpath pathName.                                                        

    ].
    ^ LibraryPath 

    "
    LibraryPath := nil.
    LLVMConfig libraryPath
    "

    "Created: / 07-08-2015 / 14:04:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 13-09-2015 / 07:01:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMConfig class methodsFor:'documentation'!

version_HG
    ^ '$Changeset: <not expanded> $'

! !


LLVMConfig initialize!