From: Daniel Berger Date: 2007-08-30T11:21:38+09:00 Subject: Re: Whatever happened to have_const? Nobuyoshi Nakada wrote: > Hi, > > At Thu, 30 Aug 2007 05:29:57 +0900, > Daniel Berger wrote in [ruby-core:12027]: >> What ever happened to the have_const patch that was supposed to get >> added to mkmf.rb? > > Sorry, but nothing yet. > >> I ask because I've hit yet another scenario where I need to check for an >> enum value. >> >> BTW, there's a bug in that diff - the word "type" should be replaced >> with "const". > > For the case of non-integral value as asked by Matz in > [ruby-core:04428], I'd change the check code like this. > > If no objection, I want to commit it to 1.8 and trunk. > Any thoughts? Looks good! > def try_const(const, header = nil, opt = "", &b) > const, type = *const > if try_compile(<<"SRC", opt, &b) > #{COMMON_HEADERS} > #{cpp_include(header)} > /*top*/ > typedef #{type || 'int'} conftest_type; > conftest_type conftestval = #{type ? '' : '(int)'}#{const}; > SRC > $defs.push(format("-DHAVE_CONST_%s", const.strip.upcase.tr_s("^A-Z0-9_", "_"))) > true > else > false > end > end > > # Returns whether or not the constant +const+ is defined. You may > # optionally pass the +type+ of +const+ as [const, type], > # like as: > # > # have_const(%w[PTHREAD_MUTEX_INITIALIZER pthread_mutex_t], "pthread.h") > # > # You may also pass additional +headers+ to check against in addition > # to the common header files, and additional flags to +opt+ which are > # then passed along to the compiler. > # > # If found, a macro is passed as a preprocessor constant to the compiler using > # the type name, in uppercase, prepended with 'HAVE_CONST_'. > # > # For example, if have_const('foo') returned true, then the HAVE_CONST_FOO > # preprocessor macro would be passed to the compiler. > # > def have_const(const, header = nil, opt = "", &b) > checking_for checking_message([*const].compact.join(' '), header, opt) do > try_const(const, header, opt, &b) > end > end Should it be "header" (singular) or "headers" (plural)? If more than one header is allowed, I think a cpp_include is required in the try_const method. If not, then the docs for have_const need a minor adjustment. Regards, Dan