[CMake] $ENV{HOSTNAME}

James Bigler bigler at cs.utah.edu
Wed Nov 21 10:05:48 EST 2007



Salvatore Iovene wrote:
> On Nov 21, 2007 4:46 PM, James Bigler <bigler at cs.utah.edu> wrote:
>>>>> shell-independent way. Maybe there's a way to execute the `hostname`
>>>>> command?
>>> Ok, I see now. HOSTNAME is a shell variable, but not an environment
>>> variable. Check this thread here:
>>>
>>> http://lists.freebsd.org/pipermail/freebsd-questions/2004-July/052424.html
>>>
>>> So, is there another way I could get the hostname in CMake?
>> I would do what was suggested and call 'hostname' with EXECUTE_PROCESS()
>> (see cmake documentation).
> 
> Thanks, that works, but the OUTPUT_VARIABLE has a trailing \n. How can
> I get rid of it?

Try using a string replacement or the OUTPUT_STRIP_TRAILING_WHITESPACE 
argument to EXECUTE_PROCESS:

################################################3
###  Run with cmake -P hostname.cmake

EXECUTE_PROCESS( COMMAND hostname
                  OUTPUT_VARIABLE myhostname
                 )

MESSAGE("myhostname = \"${myhostname}\"")

STRING(REPLACE "\n" "" new_hostname ${myhostname})

MESSAGE("new_hostname = \"${new_hostname}\"")

# Or use the handy dandy OUTPUT_STRIP_TRAILING_WHITESPACE argument
# to EXECUTE_PROCESS
EXECUTE_PROCESS( COMMAND hostname
                  OUTPUT_VARIABLE myhostname2
                  OUTPUT_STRIP_TRAILING_WHITESPACE
                 )

MESSAGE("myhostname2 = \"${myhostname2}\"")

####################################################

James


More information about the CMake mailing list